psf/requests

urllib3.exceptions.LocationParseError: Failed to parse

Closed this issue · 2 comments

python3 -c "import requests; requests.get('https://google.com');" produces error.

Expected Result

requests.get() gets the URL in question.

Actual Result

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/requests/models.py", line 379, in prepare_url
    scheme, auth, host, port, path, query, fragment = parse_url(url)
  File "/usr/lib/python3/dist-packages/urllib3/util/url.py", line 392, in parse_url
    return six.raise_from(LocationParseError(source_url), None)
  File "<string>", line 3, in raise_from
urllib3.exceptions.LocationParseError: Failed to parse: https://google.com

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 519, in request
    prep = self.prepare_request(req)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 452, in prepare_request
    p.prepare(
  File "/usr/lib/python3/dist-packages/requests/models.py", line 313, in prepare
    self.prepare_url(url, params)
  File "/usr/lib/python3/dist-packages/requests/models.py", line 381, in prepare_url
    raise InvalidURL(*e.args)
requests.exceptions.InvalidURL: Failed to parse: https://google.com

Reproduction Steps

python3 -c "import requests; requests.get('https://google.com');" on CLI.

System Information

$ python3 -m requests.help
{
  "chardet": {
    "version": "3.0.4"
  },
  "cryptography": {
    "version": "2.8"
  },
  "idna": {
    "version": "2.8"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.8.2"
  },
  "platform": {
    "release": "5.4.0-33-generic",
    "system": "Linux"
  },
  "pyOpenSSL": {
    "openssl_version": "1010104f",
    "version": "19.0.0"
  },
  "requests": {
    "version": "2.22.0"
  },
  "system_ssl": {
    "version": "1010106f"
  },
  "urllib3": {
    "version": "1.25.8"
  },
  "using_pyopenssl": true
}

Other Information

When I try to hit that URL in my browser it works fine.

I am seeing the same thing with Certbot and Emscripten.

Reproduction Steps
python3 -c "import requests; requests.get('https://google.com');" on CLI.

I can't reproduce this on Linux (Debian), Mac (OS X Catalina), or Windows (10). Likely something wrong on your side.

I can reproduce this error.
This error ocurred in my installation because urllib3/util/url.py catches the following error and transforms it into InvalidURL:

module 'six' has no attribute 'ensure_str'

Somewhere in the dependency chain a not-recent-enough version of six is requested. In my case 1.14.0 was installed, but ensure_str seems to be available from 1.15.0. So this could help (it is a workaround of course):

pip3 install six==1.15.0