问题描述
我正在使用paho.mqtt库与AWS Iot Core进行连接,但无法连接,并且出现以下错误
ssl.SSLError:[X509]找不到证书或crl(_ssl.c:4062)
任何人都可以帮助我解决这个问题。
import paho.mqtt.client as paho
import os
import socket
import ssl
from time import sleep
from random import uniform
connflag = False
def on_connect(client,userdata,flags,rc): # func for making connection
global connflag
print ("Connected to AWS")
connflag = True
print("Connection returned result: " + str(rc) )
def on_message(client,msg): # Func for Sending msg
print(msg.topic+" "+str(msg.payload))
mqttc = paho.Client() # mqttc object
mqttc.on_connect = on_connect # assign on_connect func
mqttc.on_message = on_message # assign on_message func
#mqttc.on_log = on_log
#### Change following parameters ####
awshost = "endpoint" # Endpoint
awsport = 8883 # Port no.
clientId = "nodemcu" # Thing_Name
thingName = "nodemcu" # Thing_Name
caPath = "C:/Users/pc/Desktop/instagram/aws_lambda/certi/public.pem.key"
# Root_CA_Certificate_Name
certPath = "C:/Users/pc/Desktop/instagram/aws_lambda/certi/certificate.pem.crt"
# <Thing_Name>.cert.pem
keyPath = "C:/Users/pc/Desktop/instagram/aws_lambda/certi/private.pem.key" #
<Thing_Name>.private.key
mqttc.tls_set(caPath,certfile=certPath,keyfile=keyPath,cert_reqs=ssl.CERT_required,tls_version=ssl.PROTOCOL_TLSv1_2,ciphers=None) # pass parameters
mqttc.connect(awshost,awsport,keepalive=60) # connect to aws server
mqttc.loop_start() # Start the loop
while 1==1:
sleep(5)
if connflag == True:
tempreading = uniform(20.0,25.0) # Generating Temperature Readings
mqttc.publish("temperature",tempreading,qos=1) # topic: temperature # Publishing
Temperature values
print("msg sent: temperature " + "%.2f" % tempreading ) # Print sent temperature msg on
console
else:
print("waiting for connection...")
谢谢
解决方法
caPath肯定不正确。它必须是Amazon的根目录 您可以在Google上搜索。
,我通过从 https://docs.aws.amazon.com/iot/latest/developerguide/server-authentication.html#server-authentication-certs 获取最新的 Amazon root ca 解决了这个问题