Files
python----/mqtttest.ipynb
2025-10-20 12:32:18 +08:00

8.5 KiB

In [1]:
import time
import paho.mqtt.client as mqtt
client = None
def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("连接成功")
        print("Connected with result code " + str(rc))
        print(userdata)
        print(client)
        print(flags)
def on_message(client, userdata, msg):
    print(msg.payload)
    print(userdata)
    # client.publish(topic="/gsm_rfid_reader/id865860043897629/register_ack", payload="message", qos=0)
    # print("发送了1条消息")
client = mqtt.Client(client_id="xtl")
client.username_pw_set("admin", "password")
client.on_connect = on_connect
client.on_message = on_message
client.connect(host="139.159.254.43", port = 42070, keepalive=60)  # 订阅频道
time.sleep(1)
client.subscribe(topic="/gsm_rfid_reader/device/register")
client.subscribe(topic="/gsm_rfid_reader/report/card_data_all")
client.subscribe(topic="/gsm_rfid_reader/report/card_data_gps")
client.subscribe(topic="/json/sys/865860043897629/rrpc/request/state/ack")
client.loop_forever()
---------------------------------------------------------------------------
ConnectionRefusedError                    Traceback (most recent call last)
Cell In[1], line 20
     18 client.on_connect = on_connect
     19 client.on_message = on_message
---> 20 client.connect(host="222.187.254.43", port = 42070, keepalive=60)  # 订阅频道
     21 time.sleep(1)
     22 client.subscribe(topic="/gsm_rfid_reader/device/register")

File d:\Anaconda3\Lib\site-packages\paho\mqtt\client.py:914, in Client.connect(self, host, port, keepalive, bind_address, bind_port, clean_start, properties)
    910         raise ValueError("Properties only apply to MQTT V5")
    912 self.connect_async(host, port, keepalive,
    913                    bind_address, bind_port, clean_start, properties)
--> 914 return self.reconnect()

File d:\Anaconda3\Lib\site-packages\paho\mqtt\client.py:1044, in Client.reconnect(self)
   1041 # Put messages in progress in a valid state.
   1042 self._messages_reconnect_reset()
-> 1044 sock = self._create_socket_connection()
   1046 if self._ssl:
   1047     # SSL is only supported when SSLContext is available (implies Python >= 2.7.9 or >= 3.2)
   1049     verify_host = not self._tls_insecure

File d:\Anaconda3\Lib\site-packages\paho\mqtt\client.py:3685, in Client._create_socket_connection(self)
   3683     return socks.create_connection(addr, timeout=self._connect_timeout, source_address=source, **proxy)
   3684 else:
-> 3685     return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)

File d:\Anaconda3\Lib\socket.py:853, in create_connection(address, timeout, source_address, all_errors)
    851 try:
    852     if not all_errors:
--> 853         raise exceptions[0]
    854     raise ExceptionGroup("create_connection failed", exceptions)
    855 finally:
    856     # Break explicitly a reference cycle

File d:\Anaconda3\Lib\socket.py:838, in create_connection(address, timeout, source_address, all_errors)
    836 if source_address:
    837     sock.bind(source_address)
--> 838 sock.connect(sa)
    839 # Break explicitly a reference cycle
    840 exceptions.clear()

ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。
In [ ]:
import paho.mqtt.client as mqtt
import time
import sys
def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))
def on_publish(self,one,two):
    print("消息发送成功")
client = mqtt.Client(client_id="1")
client.username_pw_set("admin", "password")
client.on_connect = on_connect
client.on_publish= on_publish
client.connect(host="222.187.254.43", port=43703, keepalive=60)  # 订阅频道
time.sleep(1)
i = 0
while True:
   
       
        data = "message" + str(i)
        client.publish(topic="Top", payload=data, qos=0)
        time.sleep(5)
        i += 1
        if(i == 100):
            print("发送了100条消息了,退出")
            client.disconnect()
           
In [ ]:
import datetime
datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')