简体   繁体   中英

Invalid URL error is raised when running azure iotc.connect() from within a Python:3.7 base image docker container

My script runs correctly and can connect to IOT central when ran on the host RaspberryPi, Python3.7 and also from my Windows10 laptop. However, it raises an InvalidURL(f"URL can't contain control characters. {url:r} error when ran in a container built with the python.3.7-buster.

I have tried various Python Docker images but got the same error.

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

I expect a normal connection to IOT central but instead get

InvalidURL(f"URL can't contain control characters. {url!r} "

Issue was reported here and resolved in version 0.3.5 of the iotc package.

I had the same problem, be sure to use Python 3.7, PIP3 and install iotc with PIP3 and it worked, in my case it had multiple versions and I had installed iotc with Python2.7

pip3 install iotc

Adding resolution here for reference:

Version 0.3.4 works well when running on python 2.7 and gives no error when accessing the URL.

Migrate from single http header string to object of headers. This fixes URL parsing error with python 3+ and latest http lib.

For more details refer: IoTC python client connect exception: InvalidURL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM