解决方法失败:[SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败(_ssl.c:661)错误

问题描述 投票:0回答:1

我正在尝试使用mqtt协议和raspberry pi 3将实时传感器数据发送到AWS IoT因此,当我在终端iam中运行我的代码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 python代码

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))

#def on_log(client, userdata, level, buf):
#    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 = "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...")

任何人都可以提供帮助吗????

请帮助

  1. 列表项
raspberry-pi3 aws-iot
1个回答
-1
投票

我也面临同样的问题。

这种方法帮助我解决了问题

https://github.com/PyGithub/PyGithub/issues/589#issuecomment-327006870

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.