Browse Source

add a very simple example client

William Pitcock 2 years ago
parent
commit
191450cf9b
1 changed files with 27 additions and 0 deletions
  1. 27
    0
      client.py

+ 27
- 0
client.py View File

@@ -0,0 +1,27 @@
1
+import asyncio
2
+import sys
3
+import uvloop
4
+
5
+
6
+asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
7
+loop = asyncio.get_event_loop()
8
+
9
+
10
+from asyncmqtt.client import MQTTClientProtocol
11
+from asyncmqtt.packet_publish import PublishPacket
12
+
13
+
14
+async def main():
15
+     print('starting')
16
+     conn, client = await loop.create_connection(MQTTClientProtocol, sys.argv[1], int(sys.argv[2]))
17
+     client.connect(sys.argv[3], sys.argv[4])
18
+     client.subscribe([(sys.argv[5] + '/#', 0)])
19
+
20
+     while True:
21
+         message = await client.next_message()
22
+         if type(message) == PublishPacket:
23
+             print(message.variable_header.topic_name, '=>', message.payload.data.decode('utf-8'))
24
+
25
+
26
+asyncio.ensure_future(main())
27
+loop.run_forever()