AWSIoTPythonSDK连接websocket握手错误

时间:2018-10-16 19:42:17

标签: python aws-iot

我最初问一个问题,即AWS IoT Python SDK是否需要根CA,因为我的连接尝试将超时而不会出错。添加根CA片段后,现在出现Websocket握手错误。有谁知道这可能是什么问题?下面的代码以及我的run语句。我正在使用AmazonRootCA1.pem文件。该代码基本上是Cognito示例,但是直接传递秘密访问密钥和访问密钥ID,而不是从Cognito ID池中提取。其他人可以成功运行吗?

Python版本:Python 3.6.5 :: Anaconda,Inc。

还要注意,我有一个ats端点,因此我遵循this link来修改cores.py文件,因此我正在构建AWSIoTPythonSDK的本地版本。但这是唯一的变化。

python test-script.py -e '<endpoint>' -t 'my/test/topic' -aki '<access key id>' -sak '<secret access key' -r 'AmazonRootCA1.pem'

test-script.py:

import argparse
import logging
import time
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient


# Custom MQTT message callback
def customCallback(client, userdata, message):
    print("Received a new message: ")
    print(message.payload)
    print("from topic: ")
    print(message.topic)
    print("--------------\n\n")


# Read in command-line parameters
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--endpoint", action="store", required=True, dest="host", help="Your AWS IoT custom endpoint")
parser.add_argument("-r", "--rootCA", action="store", required=True, dest="rootCAPath", help="Root CA file path")
parser.add_argument("-aki", "--accessKeyId", action="store", dest="accessKeyId", help="AWS Access Key ID")
parser.add_argument("-sak", "--secretAccessKey", action="store", dest="secretAccessKey", help="AWS Secret Access Key")
parser.add_argument("-id", "--clientId", action="store", dest="clientId", default="basicPubSub_CognitoSTS",
                    help="Targeted client id")
parser.add_argument("-t", "--topic", action="store", dest="topic", default="sdk/test/Python", help="Targeted topic")

args = parser.parse_args()
host = args.host
rootCAPath = args.rootCAPath
clientId = args.clientId
AccessKeyId = args.accessKeyId
SecretKey = args.secretAccessKey
topic = args.topic

# Configure logging
logger = logging.getLogger("AWSIoTPythonSDK.core")
logger.setLevel(logging.DEBUG)
streamHandler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
streamHandler.setFormatter(formatter)
logger.addHandler(streamHandler)

# Init AWSIoTMQTTClient
myAWSIoTMQTTClient = AWSIoTMQTTClient(clientId, useWebsocket=True)

# AWSIoTMQTTClient configuration
myAWSIoTMQTTClient.configureEndpoint(host, 443)
myAWSIoTMQTTClient.configureCredentials(rootCAPath)
myAWSIoTMQTTClient.configureIAMCredentials(AccessKeyId, SecretKey)
myAWSIoTMQTTClient.configureAutoReconnectBackoffTime(1, 32, 20)
myAWSIoTMQTTClient.configureOfflinePublishQueueing(-1)  # Infinite offline Publish queueing
myAWSIoTMQTTClient.configureDrainingFrequency(2)  # Draining: 2 Hz
myAWSIoTMQTTClient.configureConnectDisconnectTimeout(10)  # 10 sec
myAWSIoTMQTTClient.configureMQTTOperationTimeout(5)  # 5 sec

# Connect and subscribe to AWS IoT
myAWSIoTMQTTClient.connect()
myAWSIoTMQTTClient.subscribe(topic, 1, customCallback)
time.sleep(2)

# Publish to the same topic in a loop forever
loopCount = 0
while True:
    myAWSIoTMQTTClient.publish(topic, "New Message " + str(loopCount), 1)
    loopCount += 1
    time.sleep(1)

0 个答案:

没有答案