[issue34038] urllib2.urlopen fails if http_proxy(s) is set to a sock5 proxy

Daniel Wallace report at bugs.python.org
Sat May 4 11:34:53 EDT 2019


Daniel Wallace <daniel.wallace12 at gmail.com> added the comment:

I think this is caused by the fact that socks5 proxies are not supported?

$ cat ~/issue34038.py
from urllib.request import urlopen, HTTPError

url = 'http://icanhazip.com'
u = urlopen(url)

$./python.exe ~/issue34038.py
Traceback (most recent call last):
  File "/Users/dwallace/issue34038.py", line 4, in <module>
    u = urlopen(url)
  File "/Users/dwallace/workspace/cpython/Lib/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/Users/dwallace/workspace/cpython/Lib/urllib/request.py", line 524, in open
    response = self._open(req, data)
  File "/Users/dwallace/workspace/cpython/Lib/urllib/request.py", line 541, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/Users/dwallace/workspace/cpython/Lib/urllib/request.py", line 502, in _call_chain
    result = func(*args)
  File "/Users/dwallace/workspace/cpython/Lib/urllib/request.py", line 804, in <lambda>
    meth(r, proxy, type))
  File "/Users/dwallace/workspace/cpython/Lib/urllib/request.py", line 832, in proxy_open
    return self.parent.open(req, timeout=req.timeout)
  File "/Users/dwallace/workspace/cpython/Lib/urllib/request.py", line 524, in open
    response = self._open(req, data)
  File "/Users/dwallace/workspace/cpython/Lib/urllib/request.py", line 546, in _open
    return self._call_chain(self.handle_open, 'unknown',
  File "/Users/dwallace/workspace/cpython/Lib/urllib/request.py", line 502, in _call_chain
    result = func(*args)
  File "/Users/dwallace/workspace/cpython/Lib/urllib/request.py", line 1386, in unknown_open
    raise URLError('unknown url type: %s' % type)
urllib.error.URLError: <urlopen error unknown url type: socks5>

Though the error message could be better.

You can work around this by setting the default socket.

import urllib.request
import socket
import socks

url = 'http://icanhazip.com'
socks.set_default_proxy(socks.SOCKS5, "localhost",port=8888)
socket.socket = socks.socksocket
print(urllib.request.urlopen(url).read())

----------
nosy: +Daniel Wallace

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34038>
_______________________________________


More information about the Python-bugs-list mailing list