all Python 3http.clientexception
http.client.InvalidURL
exceptions
An error in the address: a non-numeric or empty port, control characters or a space in the request path. A subclass of HTTPException.
Syntax
raise http.client.InvalidURL(message)
The most common cause is a scheme in the host name: HTTPConnection('http://example.com') sees the port '//example.com'. The second is a space or newline in the path: http.client rejects them to prevent header injection, so encode the path with urllib.parse.quote(). The error happens before any networking, when the connection is created or in putrequest().
Examples
import http.client
from urllib.parse import quote
try:
http.client.HTTPConnection('example.com:abc')
except http.client.InvalidURL as e:
print(e) # → nonnumeric port: 'abc'
conn = http.client.HTTPConnection('example.com')
try:
conn.putrequest('GET', '/my file')
except http.client.InvalidURL:
print('use', quote('/my file')) # → use /my%20file
See also
Open in the full glossarydocs.python.orgReport an inaccuracy