求助:使用python3的paho.mqtt在client.connect这边报错
本帖最后由 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' 电脑是win10 64位系统,安装paho.mqtt的时候是使用cmd进入Python的根目录然后用pip install paho-mqtt安装的 Python版本换成3.7.9 代码一行没动,成功跑起来。。。。。。。。。
页:
[1]