使用 http.client 时如何修复无效的 URL 错误?

How to fix invalid URL error while using http.client?

我正在尝试使用 pyhton3 的 http.client 来点击 api 来模拟我在网络浏览器中如何做同样的事情。 不过http.client觉得url不合适

这就是我想要做的。

import http.client

connection = http.client.HTTPSConnection("https://analyticsapi.zoho.com/api/EmailAddress/WorkspaceName/TableName?ZOHO_ACTION=IMPORT&ZOHO_OUTPUT_FORMAT=XML&ZOHO_ERROR_FORMAT=XML&ZOHO_API_VERSION=1.0&authtoken=************&ZOHO_IMPORT_TYPE=APPEND&ZOHO_AUTO_IDENTIFY=TRUE&ZOHO_ON_IMPORT_ERROR=ABORT&ZOHO_CREATE_TABLE=TRUE&ZOHO_FILE=/home/dev1/Desktop/Zoho/temporary.csv")
connection.request("GET", "/")
response = connection.getresponse()
print("Status: {} and reason: {}".format(response.status, response.reason))

connection.close()

这就是我遇到的错误。

$ python3 pyToTestPushingCSV.py 
Traceback (most recent call last):
  File "/usr/lib/python3.5/http/client.py", line 798, in _get_hostport
    port = int(host[i+1:])
ValueError: invalid literal for int() with base 10: '//analyticsapi.zoho.com/api/usename/ATable/InsideTable?ZOHO_ACTION=IMPORT&ZOHO_OUTPUT_FORMAT=XML&ZOHO_ERROR_FORMAT=XML&ZOHO_API_VERSION=1.0&authtoken=****

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "pyToTestPushingCSV.py", line 3, in <module>
    connection = http.client.HTTPSConnection("https://analyticsapi.zoho.com/api/usename/ATable/InsideTable?ZOHO_ACTION=IMPORT&ZOHO_OUTPUT_FORMAT=XML&ZOHO_ERROR_FORMAT=XML&ZOHO_API_VERSION=1.0&authtoken=****************&ZOHO_IMPORT_TYPE=APPEND&ZOHO_AUTO_IDENTIFY=TRUE&ZOHO_ON_IMPORT_ERROR=ABORT&ZOHO_CREATE_TABLE=TRUE&ZOHO_FILE=/home/dev1/Desktop/Zoho/temporary.csv")
  File "/usr/lib/python3.5/http/client.py", line 1233, in __init__
    source_address)
  File "/usr/lib/python3.5/http/client.py", line 762, in __init__
    (self.host, self.port) = self._get_hostport(host, port)
  File "/usr/lib/python3.5/http/client.py", line 803, in _get_hostport
    raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
http.client.InvalidURL: nonnumeric port: '//analyticsapi.zoho.com/api/usename/ATable/InsideTable?ZOHO_ACTION=IMPORT&ZOHO_OUTPUT_FORMAT=XML&ZOHO_ERROR_FORMAT=XML&ZOHO_API_VERSION=1.0&authtoken=*************&ZOHO_IMPORT_TYPE=APPEND&ZOHO_AUTO_IDENTIFY=TRUE&ZOHO_ON_IMPORT_ERROR=ABORT&ZOHO_CREATE_TABLE=TRUE&ZOHO_FILE=/home/dev1/Desktop/Zoho/temporary.csv'

当我用浏览器点击 URL 时,它在 XML 中给出了良好的响应,简而言之,成功了。 这是我参考文档的地方 你能指出我哪里出错了吗?

根据 documentation 只有在 Python 编译时加入了 SSL 支持(通过 ssl 模块),HTTPS 支持才可用。

此外,https 连接的默认端口是 443。模块似乎默认访问该端口。