mqtt_server.py 710 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: AnsjerFormal
  7. @software: PyCharm
  8. @DATE: 2019/3/7 9:42
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: mqtt_server.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. import paho.mqtt.client as mqtt
  15. def on_connect(client, userdata, flags, rc):
  16. print("Connected with result code " + str(rc))
  17. client.subscribe("chat")
  18. def on_message(client, userdata, msg):
  19. print(msg.topic + " " + ":" + str(msg.payload))
  20. client = mqtt.Client()
  21. client.on_connect = on_connect
  22. client.on_message = on_message
  23. client.connect("192.168.136.45", 1883, 60)
  24. client.loop_forever()