【问题标题】:Invalid URL error is raised when running azure iotc.connect() from within a Python:3.7 base image docker container从 Python:3.7 基础映像 docker 容器中运行 azure iotc.connect() 时引发无效 URL 错误
【发布时间】:2020-02-28 22:05:48
【问题描述】:

我的脚本运行正确,并且在主机 RaspberryPi、Python3.7 以及我的 Windows10 笔记本电脑上运行时可以连接到 IOT Central。但是,在使用 python:3.7-buster 构建的容器中运行时,它会引发 InvalidURL(f"URL 不能包含控制字符。{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} "

【问题讨论】:

    标签: python docker-image azure-iot-central


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,请务必使用 Python 3.7、PIP3 并使用 PIP3 安装 iotc,它可以工作,在我的情况下它有多个版本,我已经使用 Python2.7 安装了 iotc

      pip3 安装 iotc

      【讨论】:

        【解决方案3】:

        在此处添加分辨率以供参考:

        0.3.4 版在 python 2.7 上运行良好,访问 URL 时没有错误。

        从单个 http 标头字符串迁移到标头对象。 这修复了 python 3+ 和最新的 http lib 的 URL 解析错误。

        更多详情请参考:IoTC python client connect exception: InvalidURL

        【讨论】:

          猜你喜欢
          • 2019-10-04
          • 2018-02-02
          • 2021-03-24
          • 2021-12-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-21
          • 1970-01-01
          • 2022-08-10
          相关资源
          最近更新 更多