pythonpython-3.xhttp.client

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


I am trying to hit an api using http.client of pyhton3 to simulate how I would do the same in a web browser. However, http.client feels that url is inappropriate.

This is what I am trying to do.

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()

And this is the error I am getting.

$ 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'

When I hit the URL with my Browser it gives a good response back in XML which in short, succeeds. This is where I referred for documentation Can you pin point where I went wrong?


Solution

  • As per documentation HTTPS support is only available if Python was compiled with SSL support (through the ssl module).

    Also the default port for https connection is 443. The module seems to be hitting that port by default.