zython
zython

Reputation: 1288

Requests: Can't convert 'NoneType' object to str implicitly

I am trying to perform a GET with requests with this url:

http://g.api.mega.co.nz

when requests gives me the the following error:

>>> r = requests.get("http://g.api.mega.co.nz")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/requests/api.py", line 67, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/api.py", line 53, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 468, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 597, in send
    history = [resp for resp in gen] if allow_redirects else []
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 597, in <listcomp>
    history = [resp for resp in gen] if allow_redirects else []
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 195, in resolve_redirects
    **adapter_kwargs
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 576, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 343, in send
    conn = self.get_connection(request.url, proxies)
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 250, in get_connection
    proxy = select_proxy(url, proxies)
  File "/usr/lib/python3/dist-packages/requests/utils.py", line 575, in select_proxy
    proxy = proxies.get(urlparts.scheme+'://'+urlparts.hostname)
TypeError: Can't convert 'NoneType' object to str implicitly
>>> 

I have tested it in python2.7 and python3.5 both give semantically the same error. I thought that the long subdomain chain gives the error but I disproved that by testing with a long subdomain chain on my server with a wildcard dns configuration.

I can see with host g.api.mega.co.nz that the url is an alias for lu.api.mega.co.nz.

Trying to acces that url with curl --headand http gives me the following:

HTTP/1.1 301 Moved Permanently
Content-Length: 0
Location: https://
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type, MEGA-Chrome-Antileak
Access-Control-Expose-Headers: Original-Content-Length
Access-Control-Max-Age: 86400

When I try to acces with https because of the 301 status code I get an empty response.

The plot thickens.

Upvotes: 0

Views: 1074

Answers (1)

zython
zython

Reputation: 1288

This error occurs when services fail to redirect to strictly speaking invalid locations, according to the requests/urllib library.

Simliar: LocationValueError when using python requests module


Every time I encountered this the service offered https which turns out to be the root of this problem.

In example above the service redirects to Location: https:// where requests will parse the hostname to None which causes the error.

In another example sensetime.com the URL redirects to:

...
< Connection: keep-alive
< Location: https:www.sensetime.com/
<
...

which urllib cannnot parse (because of the missing backslashes and fails.

What you should do/What I did:

  • do a catch-all and put those url's where you encounter this in a separate place where you can handle them later to connect via https

  • update your requests library(pip install --upgrade requests): the newest for me available version gave me a different error message, which allowed me to find the answer to this problem I had

Upvotes: 1

Related Questions