You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过MQTT向Azure IoT Hub的X509认证设备发送数据时SSL验证失败

Paho MQTT连接Azure IoT Hub时出现SSL_Verification_failed错误(CA签名证书)

我正在尝试通过CA签名的X509证书让设备向Azure IoT Hub发送传感器数据,已经完成了CA证书、设备证书及密钥的生成流程。现在直接使用Paho MQTT库连接Hub(未使用Azure IoT SDK),但运行代码后一直报SSL_Verification_failed错误。

我的代码如下:

from paho.mqtt import client as mqtt
import ssl

path_to_root_cert = "<local path to the generated testonly-rootca.pem>"
device_cert = "<local path to the generated newdevice-cert.pem>"
device_key = "<local path to the generated newdevice-key.pem>"
HubName = "iothub.azure-devices.net"
devicename = "device001"

def on_connect(client, userdata, flags, rc):
    print ("Connected with result code: " + str(rc))
    client.subscribe("devices/" + devicename + "/messages/devicebound/#")

def on_disconnect(client, userdata, rc):
    print ("Disconnected with result code: " + str(rc))

def on_message(client, userdata, msg):
    print (msg.topic+" "+str(msg.payload))
    client.publish("devices/" + devicename + "/messages/events/", "{id=1}",qos=1)

def on_publish(client, userdata, mid):
    print ("Sent message")

client = mqtt.Client(client_id=devicename, protocol=mqtt.MQTTv311)
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message
client.on_publish = on_publish

client.username_pw_set(username=HubName + "/" + devicename, password=None)
client.tls_insecure_set(False)
client.tls_set(ca_certs=path_to_root_cert, 
               certfile=device_cert, 
               keyfile=device_key, 
               cert_reqs=ssl.CERT_REQUIRED, 
               tls_version=ssl.PROTOCOL_TLSv1_2, 
               ciphers=None)

client.connect(HubName, port=8883)
client.publish("devices/" + devicename + "/messages/events/", "{id=MQTT Test}", qos=1)
client.loop_forever()

运行后得到的错误输出:

SSL_Verification_failed

我已经确认证书文件路径都是正确的,想知道哪里出了问题,该怎么解决这个SSL验证失败的问题?

内容的提问来源于stack exchange,提问作者Ganesh Eswaran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:01:01