我的脚本运行正常,并且在主机 RaspberryPi、Python3.7 以及我的 Windows10 笔记本电脑上运行时可以连接到 IOT 中心。但是,在使用 python:3.7-buster 构建的容器中运行时,它会引发 InvalidURL(f"URL can't contain control characters. {url!r} 错误。
我尝试了各种 Python Docker 映像,但得到了相同的错误。
import iotc
from iotc import IOTConnectType, IOTLogLevel
from random import randint
import settings, client
import json, time
# settings_fp = r"config.json"
settings_fp = '/var/lib/project/config.json'
with open(settings_fp, 'r') as settings_file:
settings = json.load(settings_file)
scopeId = settings['cloud']['scope_id']
deviceId = settings['cloud']['device_id']
deviceKey = settings['cloud']['primary_key']
iotc = iotc.Device(scopeId, deviceKey, deviceId, IOTConnectType.IOTC_CONNECT_SYMM_KEY)
iotc.setLogLevel(IOTLogLevel.IOTC_LOGGING_API_ONLY)
gCanSend = False
gCounter = 0
def onconnect(info):
global gCanSend
print("- [onconnect] => status:" + str(info.getStatusCode()))
if info.getStatusCode() == 0:
if iotc.isConnected():
gCanSend = True
print("Connected to the cloud!!!")
def onmessagesent(info):
print("\t- [onmessagesent] => " + str(info.getPayload()))
def oncommand(info):
print("- [oncommand] => " + info.getTag() + " => " + str(info.getPayload()))
def onsettingsupdated(info):
print("- [onsettingsupdated] => " + info.getTag() + " => " + info.getPayload())
iotc.on("ConnectionStatus", onconnect)
iotc.on("MessageSent", onmessagesent)
iotc.on("Command", oncommand)
iotc.on("SettingsUpdated", onsettingsupdated)
iotc.connect() # code fails here
我希望能正常连接到 IOT 中心,但会得到
InvalidURL(f"URL 不能包含控制字符。{url!r}"