Interactive MQTT packet manipulation shell based on IPython You would love it if your are an expert of MQTT and Python ![screencast](./imqtt-screencast.gif) ## Installing ``` pip install imqtt ``` or installing from source code ``` python setup.py install ``` ## Working as a client ### Enter IMQTT shell ``` $ imqtt ``` ### Build a TCP Client and connect to mqtt server ``` In [1]: c = TCPClient(host = '127.0.0.1', port = 1883) ('connected to', '127.0.0.1', 1883) ``` ### Send a MQTT Connect Packet ``` In [2]: p = ConnectPacket() In [3]: c.Send(p) Out[3]: <__main__.TCPClient instance at 0x215c518> ``` ### Recv the Connack Packet ``` In [4]: ack = c.Recv() In [5]: ack Out[5]: , Flags: 0x1, ReturnCode: 0x0> ``` ### Publish a message ``` In [6]: pp = PublishPacket(Topic = '/imqtt/test', Payload = 'Hello world', QoS = 1, PacketID = 1) In [7]: pp Out[7]: , Dup: 0, QoS: 1, Retain: 0, PacketID: 1, Topic: /imqtt/test, Payload: Hello world> In [8]: c.Send(pp).Recv() Out[8]: , PacketID: 1> ``` ## Working in server mode (version 0.2.2) ### Lisen and serve ``` Welcome to imqtt ! An iteractive MQTT packet manipulation shell based on IPython. Github: https://github.com/shafreeck/imqtt In [1]: s = TCPServer(host = '127.0.0.1', port = 1883) listening on 127.0.0.1 1883 In [2]: s.Serve() ``` ### It will break and enter the interactive shell when received a packet ``` Packet received This is an interactive environment, and being triggered when there was a packet received. There are some varibles you can use: self: the tcp server object buf : current data received p : the packet that unmarshaled from buf Also, all python features are avaliable. Type 'exit()' or press 'Ctrl-D' to quit the shell. ``` ### Inspect the received packet and send ack back ``` In [1]: p Out[1]: , Protocol: bytearray(b'MQTT'), Version: 4, , KeepAlive: 300, ClientID: bytearray(b'imqtt'), Username: , Password: > In [2]: self.Send(ConnackPacket()) Out[2]: ``` ### Exit the shell and continue to serve ``` In [3]: exit() Continue to serve... ```