1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
- @AUTHOR: ASJRD018
- @NAME: AnsjerFormal
- @software: PyCharm
- @DATE: 2019/3/7 9:43
- @Version: python3.6
- @MODIFY DECORD:ansjer dev
- @file: mqtt_client.py
- @Contact: chanjunkai@163.com
- """
- # encoding: utf-8
- import paho.mqtt.client as mqtt
- def on_connect(client, userdata, flags, rc):
- print("Connected with result code "+str(rc))
- client.subscribe("chat")
- def on_message(client, userdata, msg):
- print(msg.topic+" " + ":" + str(msg.payload))
- client = mqtt.Client()
- client.on_connect = on_connect
- client.on_message = on_message
- client.connect("192.168.136.45", 1883, 60)
- client.loop_forever()
- exit()
- import paho.mqtt.client as mqtt
- HOST = "192.168.136.45"
- PORT = 1883
- def test():
- client = mqtt.Client()
- client.username_pw_set(username='我是设备aa',password='xxxxxxx')
- client.connect(HOST, PORT, 60)
- client.publish("chat", "lalalallalalalalal", 2)
- # client.loop()
- client.loop_forever()
- if __name__ == '__main__':
- test()
|