微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

连接到 Adafruit IO 时出现 SSL 证书错误

如何解决连接到 Adafruit IO 时出现 SSL 证书错误

似乎 Adafruit-io 库不适用于最新版本的 Python (3.9.1)。连接到 Adafruit IO 服务器时出现 SSL 证书错误

当我运行 Python 3.6、3.7 和 3.8 时,这段代码运行良好。

import sys
import time
from Adafruit_IO import MQTTClient      # pip install adafruit-io

ADAFRUIT_IO_KEY = 'MY_ADAFRUIT_KEY'
ADAFRUIT_IO_USERNAME = 'MY_ADAFRUIT_NAME'
IO_Feed = 'MY_Feed'

def connected(client):
    print ('Connected to Adafruit IO! Listening for Feed changes...')
    client.subscribe(IO_Feed)

def disconnected(client):
    print ('disconnect from Adafruit IO!')
    sys.exit(1)

def message(client,Feed_id,payload):
    # Feed_id or {0} represents the name of the Feed in the message to the Adafruit IO service.
    # Payload or {1} represents the value being sent.
    print ('Feed {0} recieved new vaule: {1}'.format(Feed_id,payload))

    if '{1}'.format(Feed_id,payload) == "ON":
        print("ON Worked ")
    if '{1}'.format(Feed_id,payload) == "OFF":
        print("OFF worked ")

client = MQTTClient(ADAFRUIT_IO_USERNAME,ADAFRUIT_IO_KEY)

client.on_connect    = connected
client.on_disconnect = disconnected
client.on_message    = message
client.loop_background()
client.connect()

while True:
    # publish data to the Feed here
    time.sleep(10)

我已升级到 Python 3.9.1,但出现以下连接错误

Traceback (most recent call last):
  File "/Users/ryan/Code/gdgIOT/subscribe.py",line 33,in <module>
    client.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/Adafruit_IO/mqtt_client.py",line 150,in connect
    self._client.connect(self._service_host,port=self._service_port,File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/paho/mqtt/client.py",line 941,in connect
    return self.reconnect()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/paho/mqtt/client.py",line 1104,in reconnect
    sock.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py",line 1309,in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_Failed] certificate verify Failed: self signed certificate in certificate chain (_ssl.c:1123)

非常感谢任何帮助!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。