Python requests proxy socks5 OSError: [Errno 0] Error

在使用 Python requests 设置 socks5 代理请求时,抛出异常:

OSError: [Errno 0] Error
Error:

Traceback (most recent call last):

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen

    chunked=chunked,

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request

    self._validate_conn(conn)

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connectionpool.py", line 976, in _validate_conn

    conn.connect()

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connection.py", line 370, in connect

    ssl_context=context,

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\util\ssl_.py", line 377, in ssl_wrap_socket

    return context.wrap_socket(sock, server_hostname=server_hostname)

  File "D:\work\Python\lib\ssl.py", line 412, in wrap_socket

    session=session

  File "D:\work\Python\lib\ssl.py", line 853, in _create

    self.do_handshake()

  File "D:\work\Python\lib\ssl.py", line 1117, in do_handshake

    self._sslobj.do_handshake()

OSError: [Errno 0] Error

 

During handling of the above exception, another exception occurred:

 

Traceback (most recent call last):

  File "E:\python\3.7\epub\venv\lib\site-packages\requests\adapters.py", line 449, in send

    timeout=timeout

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connectionpool.py", line 725, in urlopen

    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\util\retry.py", line 403, in increment

    raise six.reraise(type(error), error, _stacktrace)

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\packages\six.py", line 734, in reraise

    raise value.with_traceback(tb)

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen

    chunked=chunked,

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request

    self._validate_conn(conn)

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connectionpool.py", line 976, in _validate_conn

    conn.connect()

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connection.py", line 370, in connect

    ssl_context=context,

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\util\ssl_.py", line 377, in ssl_wrap_socket

    return context.wrap_socket(sock, server_hostname=server_hostname)

  File "D:\work\Python\lib\ssl.py", line 412, in wrap_socket

    session=session

  File "D:\work\Python\lib\ssl.py", line 853, in _create

    self.do_handshake()

  File "D:\work\Python\lib\ssl.py", line 1117, in do_handshake

    self._sslobj.do_handshake()

urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError(0, 'Error'))

 

During handling of the above exception, another exception occurred:

 

Traceback (most recent call last):

  File "E:/python/3.7/epub/tool.py", line 41, in main

    res = requests.get(url, headers=headers, proxies=proxies, timeout=(CONNECTION_TIMEOUT, TRANSMISSION_TIMEOUT))

  File "E:\python\3.7\epub\venv\lib\site-packages\requests\api.py", line 76, in get

    return request('get', url, params=params, **kwargs)

  File "E:\python\3.7\epub\venv\lib\site-packages\requests\api.py", line 61, in request

    return session.request(method=method, url=url, **kwargs)

  File "E:\python\3.7\epub\venv\lib\site-packages\requests\sessions.py", line 530, in request

    resp = self.send(prep, **send_kwargs)

  File "E:\python\3.7\epub\venv\lib\site-packages\requests\sessions.py", line 643, in send

    r = adapter.send(request, **kwargs)

  File "E:\python\3.7\epub\venv\lib\site-packages\requests\adapters.py", line 498, in send

    raise ConnectionError(err, request=request)

requests.exceptions.ConnectionError: ('Connection aborted.', OSError(0, 'Error'))

Error:

Traceback (most recent call last):

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen

    chunked=chunked,

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request

    self._validate_conn(conn)

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connectionpool.py", line 976, in _validate_conn

    conn.connect()

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connection.py", line 370, in connect

    ssl_context=context,

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\util\ssl_.py", line 377, in ssl_wrap_socket

    return context.wrap_socket(sock, server_hostname=server_hostname)

  File "D:\work\Python\lib\ssl.py", line 412, in wrap_socket

    session=session

  File "D:\work\Python\lib\ssl.py", line 853, in _create

    self.do_handshake()

  File "D:\work\Python\lib\ssl.py", line 1117, in do_handshake

    self._sslobj.do_handshake()

OSError: [Errno 0] Error

 

During handling of the above exception, another exception occurred:

 

Traceback (most recent call last):

  File "E:\python\3.7\epub\venv\lib\site-packages\requests\adapters.py", line 449, in send

    timeout=timeout

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connectionpool.py", line 725, in urlopen

    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\util\retry.py", line 403, in increment

    raise six.reraise(type(error), error, _stacktrace)

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\packages\six.py", line 734, in reraise

    raise value.with_traceback(tb)

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen

    chunked=chunked,

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request

    self._validate_conn(conn)

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connectionpool.py", line 976, in _validate_conn

    conn.connect()

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\connection.py", line 370, in connect

    ssl_context=context,

  File "E:\python\3.7\epub\venv\lib\site-packages\urllib3\util\ssl_.py", line 377, in ssl_wrap_socket

    return context.wrap_socket(sock, server_hostname=server_hostname)

  File "D:\work\Python\lib\ssl.py", line 412, in wrap_socket

    session=session

  File "D:\work\Python\lib\ssl.py", line 853, in _create

    self.do_handshake()

  File "D:\work\Python\lib\ssl.py", line 1117, in do_handshake

    self._sslobj.do_handshake()

urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError(0, 'Error'))

 

During handling of the above exception, another exception occurred:

 

Traceback (most recent call last):

  File "E:/python/3.7/epub/tool.py", line 41, in main

    res = requests.get(url, headers=headers, proxies=proxies, timeout=(CONNECTION_TIMEOUT, TRANSMISSION_TIMEOUT))

  File "E:\python\3.7\epub\venv\lib\site-packages\requests\api.py", line 76, in get

    return request('get', url, params=params, **kwargs)

  File "E:\python\3.7\epub\venv\lib\site-packages\requests\api.py", line 61, in request

    return session.request(method=method, url=url, **kwargs)

  File "E:\python\3.7\epub\venv\lib\site-packages\requests\sessions.py", line 530, in request

    resp = self.send(prep, **send_kwargs)

  File "E:\python\3.7\epub\venv\lib\site-packages\requests\sessions.py", line 643, in send

    r = adapter.send(request, **kwargs)

  File "E:\python\3.7\epub\venv\lib\site-packages\requests\adapters.py", line 498, in send

    raise ConnectionError(err, request=request)

requests.exceptions.ConnectionError: ('Connection aborted.', OSError(0, 'Error'))


这个问题是版本兼容性问题,指定安装版本即可:

pip uninstall requests

pip install requests==2.10.0


 

展开阅读全文