python - 从 Python :3. 7 基本镜像 docker 容器中运行 azure iotc.connect() 时,会引发无效 URL 错误

标签 python docker-image azure-iot-central

我的脚本运行正确,并且在主机 RaspberryPi、Python3.7 以及我的 Windows10 笔记本电脑上运行时可以连接到 IOT 中心。但是,当在使用 python:3.7-buster 构建的容器中运行时,它会引发 InvalidURL(f"URL can't contains 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 can't contain control characters. {url!r} "

最佳答案

已报告问题 here并在 iotc 包的 0.3.5 版本中解决。

关于python - 从 Python :3. 7 基本镜像 docker 容器中运行 azure iotc.connect() 时,会引发无效 URL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58679047/

相关文章:

azure - Microsoft IoT Central 是否通过 Azure IoT 中心发送数据...?

python - YouCompleteMe 不适用于 python

python - 应该对此 Xpath 字符串进行哪些更改才能找到最后一个子元素?

php - 如何通过使用 WSGI 执行 Python 脚本来避免 PHP exec()?

docker - Docker 镜像和容器有什么区别?

docker - 如何删除所有标记的docker镜像?

azure - 通过 MQTT 向 Azure IotCentral 发送消息

python - 如何在给定两个协程的 python 中实现,一直运行一个直到另一个完成

Docker 特定镜像不重用基础镜像层?

azure-iot-hub - 适用于 Azure IoT Central (SaaS) 的 IoT 中心设备预配服务 (DPS)