马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Fy_666 于 2021-3-10 23:24 编辑
环境 :
Python:3.9.2
paho.mqtt:1.5.1
网上几乎所有示例代码都用过,均在client.connect("xxx.xx.xx.xxx", 1883, 60)这边报错。
代码如下import paho.mqtt.client
import time
mqtt = paho.mqtt.client
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc)) # 打印连接状态
client.subscribe("2222") # 订阅2222
def on_message(client, userdata, msg):
print(msg.topic+" " + ":" + str(msg.payload)) # 打印接受的消息
client_id = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
client = mqtt.Client(client_id) # ClientId不能重复,所以使用当前时间
# 必须设置,否则会返回「Connected with result code 4」
client.username_pw_set("emqx2", "public")
client.on_connect = on_connect
client.on_message = on_message
client.connect("xxx.xx.xx.xxx", 1883, 60)
client.loop_forever()
报错如下c:
cd 'c:\Users\wx\Desktop\Python'
& 'C:\Users\wx\AppData\Local\Programs\Python\Python39\python.exe' 'c:\Users\wx\.vscode\extensions\ms-python.python-2021.2.636928669\pythonFiles\lib\python\debugpy\launcher' '56729' '--' 'c:\Users\wx\Desktop\Python\4.py'
Traceback(most recent call last):
File "c:\Users\wx\Desktop\Python\4.py", line 21, in <module >
client.connect("xxx.xx.xx.xxx", 1883, 60)
File "C:\Users\wx\AppData\Local\Programs\Python\Python39\lib\site-packages\paho\mqtt\client.py", line 941, in connect
return self.reconnect()
File "C:\Users\wx\AppData\Local\Programs\Python\Python39\lib\site-packages\paho\mqtt\client.py", line 1074, in reconnect
sock = self._create_socket_connection()
AttributeError: 'Client' object has no attribute '_create_socket_connection'
|