提交 e2ba629312fdb83598b36b2e35b8db818b47ebe7
1 个父辈
caeca211
udpate python demo
正在显示
2 个修改的文件
包含
15 行增加
和
13 行删除
1 | 1 | #!/usr/bin/env python |
2 | +#coding=utf-8 | |
2 | 3 | import hmac |
3 | 4 | import base64 |
4 | 5 | from hashlib import sha1 |
... | ... | @@ -43,6 +44,12 @@ def on_log(client, userdata, level, buf): |
43 | 44 | print('%s: %s' % (head, buf)) |
44 | 45 | def on_connect(client, userdata, flags, rc): |
45 | 46 | print('Connected with result code ' + str(rc)) |
47 | + client.subscribe(topic, 0) | |
48 | + for i in range(1, 11): | |
49 | + print(i) | |
50 | + rc = client.publish(topic, str(i), qos=0) | |
51 | + print ('rc: %s' % rc) | |
52 | + time.sleep(0.1) | |
46 | 53 | def on_message(client, userdata, msg): |
47 | 54 | print(msg.topic + ' ' + str(msg.payload)) |
48 | 55 | def on_disconnect(client, userdata, rc): |
... | ... | @@ -60,10 +67,4 @@ client.username_pw_set(userName, password) |
60 | 67 | # ssl设置,并且port=8883 |
61 | 68 | #client.tls_set(ca_certs=None, certfile=None, keyfile=None, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLS, ciphers=None) |
62 | 69 | client.connect(brokerUrl, 1883, 60) |
63 | -client.subscribe(topic, 0) | |
64 | -for i in range(1, 11): | |
65 | - print(i) | |
66 | - rc = client.publish(topic, str(i), qos=0) | |
67 | - print ('rc: %s' % rc) | |
68 | - time.sleep(0.1) | |
69 | 70 | client.loop_forever() | ... | ... |
1 | 1 | #!/usr/bin/env python |
2 | +#coding=utf-8 | |
2 | 3 | import hmac |
3 | 4 | import base64 |
4 | 5 | from hashlib import sha1 |
... | ... | @@ -43,6 +44,13 @@ def on_log(client, userdata, level, buf): |
43 | 44 | print('%s: %s' % (head, buf)) |
44 | 45 | def on_connect(client, userdata, flags, rc): |
45 | 46 | print('Connected with result code ' + str(rc)) |
47 | + client.subscribe(topic, 0) | |
48 | + for i in range(1, 11): | |
49 | + print(i) | |
50 | + ## 发送 P2P 消息,二级子 topic 是/p2p/ 三级子 topic 是目标 clientId | |
51 | + rc = client.publish(topic + '/p2p/'+ client_id, str(i), qos=0) | |
52 | + print ('rc: %s' % rc) | |
53 | + time.sleep(0.1) | |
46 | 54 | def on_message(client, userdata, msg): |
47 | 55 | print(msg.topic + ' ' + str(msg.payload)) |
48 | 56 | def on_disconnect(client, userdata, rc): |
... | ... | @@ -60,11 +68,4 @@ client.username_pw_set(userName, password) |
60 | 68 | # ssl设置,并且port=8883 |
61 | 69 | #client.tls_set(ca_certs=None, certfile=None, keyfile=None, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLS, ciphers=None) |
62 | 70 | client.connect(brokerUrl, 1883, 60) |
63 | -client.subscribe(topic, 0) | |
64 | -for i in range(1, 11): | |
65 | - print(i) | |
66 | - ## 发送 P2P 消息,二级子 topic 是/p2p/ 三级子 topic 是目标 clientId | |
67 | - rc = client.publish(topic + '/p2p/'+ client_id, str(i), qos=0) | |
68 | - print ('rc: %s' % rc) | |
69 | - time.sleep(0.1) | |
70 | 71 | client.loop_forever() | ... | ... |