【发布时间】:2019-12-13 01:56:30
【问题描述】:
我在 AWS 服务器上运行 MQTT 代理,该代理在 mac 上正常运行,而 windows 设备在我的树莓派上无法正常运行。我的订阅者和发布者代码有效,并且已经在多个操作系统上进行了测试。我相信问题出在我的树莓派设置上,但是我无法解决这个问题。
我收到的错误(使用 python3):
pi@raspberrypi:~/MQTT $ sudo python3 subscriber.py
Traceback (most recent call last):
File "subscriber.py", line 42, in <module>
client.connect(brokerAddress,port,60)
File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 937, in connect
return self.reconnect()
File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1100, in reconnect
sock.do_handshake()
File "/usr/lib/python3.7/ssl.py", line 1117, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: UNSUPPORTED_PROTOCOL] unsupported protocol (_ssl.c:1056)
我收到的 python 错误,
pi@raspberrypi:~/MQTT $ python subscriber.py
Traceback (most recent call last):
File "subscriber.py", line 42, in <module>
client.connect(brokerAddress,port,60)
File "/home/pi/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 937, in connect
return self.reconnect()
File "/home/pi/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 1100, in reconnect
sock.do_handshake()
File "/usr/lib/python2.7/ssl.py", line 828, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: UNSUPPORTED_PROTOCOL] unsupported protocol (_ssl.c:727)
我尝试了几件事,包括使用 python 与 python3、确保安装最新版本的 paho、重新启动、在另一个网络上运行树莓派、以 sudo 运行,以及其他一些事情。
这可能无关,但我之前尝试在这个树莓派上运行 git clone 时遇到过问题,我认为这可能与 ssl 问题有关。
其他可能有用的东西
pi@raspberrypi:~/MQTT $ openssl version
OpenSSL 1.1.1d 10 Sep 2019
代码,subscriber.py:
import paho.mqtt.client as mqtt
import sys
import time
import ssl
# The callback for when the client receives a conack response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("test")
printf("subscribed to test")
# When a message is received
def on_message(client, userdata, msg):
print("INCOMING") #begin message
print("TOPIC: \t\t"+msg.topic+"\nMESSAGE:\t"+str(msg.payload.decode()))
client = mqtt.Client("C1")
#declare loc of tls certificate
client.tls_set('/home/pi/MQTT/ca.crt',cert_reqs=ssl.CERT_NONE)
client.tls_insecure_set(True)
client.on_connect = on_connect
client.on_message = on_message
#plain text credentials
client.username_pw_set("myusername","mypassword")
#declare address and port
brokerAddress="my-ipv4-ip-address"
port=8883
client.connect(brokerAddress,port,60)
#continuous loop
client.loop_forever()
当在我的 mac 上运行时,这个确切的代码可以工作,我可以从我的桌面(windows)发布这条消息,它会按预期显示。
如果您需要我提供任何其他信息来帮助解决问题,请告诉我。
【问题讨论】:
-
有时需要重新编译库代码才能在不同的操作系统上工作。您是否在 Raspberry Pi 上进行了冻结并安装?
-
这看起来是客户端和代理之间的 TLS 版本不匹配。请编辑问题以包含有关如何配置代理的更多详细信息。
-
请在您的 RPI 设备上运行此程序并在您的问题中添加响应:openssl s_client -connect server-ip-address:8883 --showcerts。可能您在 RPI 设备上的 openssl 默认禁用了一些旧的“不太安全”密码(您实际上可以启用)。
标签: python ssl raspberry-pi mqtt iot