我正在尝试将M2MQTT Paho Python客户端连接到ASW IoT,这是我在C#中成功完成的。但是,由于某些原因,此代码无法正常工作。我花了很多时间尝试各种参数。 现在,我的代码没有任何错误,只是等待服务器响应,但是没有...
顺便说一句,这些证书是从Amazon下载的,而且AmazonRootCA1.crt是Amazon Toot CA证书,其他证书(及其私钥)特定于该证书,附加策略以及我在Amazon上创建的附加内容。
我还尝试了TLSv1,并且没有成功的客户端证书...
#! /usr/bin/python3.5
import serial
import time
import datetime
import os
import socket
import ssl
import logging
import paho.mqtt.client as mqtt
import sys
print(sys.executable)
def on_disconnect(client, userdata, rc):
if rc==0:
print("client disconnected OK")
client.connected_flag=False
def on_connect(client, userdata, flags, rc):
if rc==0:
print("Connected OK")
mqtt.Client.connected_flag=True
mqtt.Client.bad_connection_params=False
else:
mqtt.Client.bad_connection_params=True
if rc==1:
print("Failed to connect. Connection refused, unacceptable protocol version. Error Code=", rc)
elif rc==2:
print("Failed to connect.Connection refused, identifier rejected. Error Code=", rc)
elif rc==3:
print("Failed to connect.Connection refused, server unavailable. Error Code=", rc)
elif rc==4:
print("Failed to connect.Connection refused, bad user name or password. Error Code=", rc)
elif rc==5:
print("Failed to connect.Connection refused, not authorized. Error Code=", rc)
def on_publish(client, userdata, mid):
if rc==0:
print("Data published OK: ", userdata)
else:
print("Failed to publish data. MessageID=", mid)
pass
broker="myendpoint-ats.iot.us-west-2.amazonaws.com"
port=8883
# 4 stands for MQTTv311
rpiclient = mqtt.Client("PahoClient-on-RPi-Gateway2", clean_session=True, userdata=None, protocol=4, transport="tcp")
caCertPath = "./AmazonRootCA1.pem"
clientCertFilePath = "./xxxxxxxxxx-certificate.pem.crt"
keyFilePath = "./xxxxxxxxxx-private.pem.key"
rpiclient.tls_set(ca_certs=caCertPath,
certfile=clientCertFilePath,
keyfile=keyFilePath,
cert_reqs=ssl.CERT_REQUIRED,
tls_version=ssl.PROTOCOL_TLSv1_2,
ciphers=None)
# User name and password not used with ASW IoT when certificate is used for authentication
#rpiclient.username_pw_set(username, password)
rpiclient.tls_insecure_set(True)
# connection flag indicates that connection was made or not
mqtt.Client.connected_flag = False
# connection parameters are incorrect: ip address, port, authentication, etc
mqtt.Client.bad_connection_params=False
#assign function to callback
rpiclient.on_connect = on_connect
#assign function to callback
rpiclient.on_publish = on_publish
# bind the disconnect callback
rpiclient.on_disconnect = on_disconnect
rpiclient.loop_start()
rpiclient.will_set("dwm/position", "Client PahoClient-on-RPi had unexpectedly disconnected", 1, True)
try:
print("Connecting to MQTT broker ",broker)
# Connect to the MQTT Broker
rpiclient.connect(broker, port)
time.sleep(1)
# Wait in a loop until we are connected
print("mqtt.Client.connected_flag={}, mqtt.Client.bad_connection_params={}", mqtt.Client.connected_flag, mqtt.Client.bad_connection_params)
while mqtt.Client.connected_flag == False and mqtt.Client.bad_connection_params == False:
print("Waiting for connection...");
time.sleep(1)
if mqtt.Client.bad_connection_params == True:
rpiclient.loop_stop()
sys.exit()
except Exception as ex:
print("Connection to MQTT Broker failed: ", ex)
rpiclient.loop_stop()
# Disconnect MQTT Client
rpiclient.disconnect()
有任何线索吗?