如何修复失败:[SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败(_ssl.c:661)错误

时间:2019-11-30 15:20:57

标签: raspberry-pi3 aws-iot

我正在尝试使用mqtt协议和raspberry pi 3将实时传感器数据发送到AWS IoT。当我在终端中运行代码(pub.py)时,出现以下错误:

Traceback (most recent call last):
  File "/home/pi/Desktop/aws/iot/pub`enter code here`.py", line 39, in <module>
    mqttc.connect(awshost, awsport, keepalive=60)               # connect to aws server
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 937, in connect
    return self.reconnect()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1100, in reconnect
    sock.do_handshake()
  File "/usr/lib/python2.7/ssl.py", line 840, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)

这是我的代码(pub.py):

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, userdata, 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

awshost = "xxxxxxxxxxxx.iot.ap-south-1.amazonaws.com"  # Endpoint
awsport = 8883  # Port no.   
clientId = "Thermo_Cloud"  # Thing_Name
thingName = "Thermo_cloud"  # Thing_Name
caPath = "/home/pi/Desktop/aws/iot/root-CA.crt"  # Root_CA_Certificate_Name
certPath = "/home/pi/Desktop/aws/iot/Thermo_Cloud.cert.pem"  # <Thing_Name>.cert.pem
keyPath = "/home/pi/Desktop/aws/iot/Thermo_Cloud.private.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...")

有人可以帮忙吗?

0 个答案:

没有答案