简体   繁体   English

Urllib3 socks5代理错误:'socks'没有属性'create_connection'

[英]Urllib3 socks5 proxy error: 'socks' has no attribute 'create_connection'

I need Robobrowser to use Tor, easiest way i thought would be like this: Robobrowser with Sessions . 我需要Robobrowser使用Tor,我认为最简单的方式就是: Robobrowser和Sessions But i encountered a strange problem with requests, or more specific urllib3: 但我遇到了一个奇怪的请求问题,或更具体的urllib3:

AttributeError: module 'socks' has no attribute 'create_connection' AttributeError:模块'socks'没有属性'create_connection'

As documented in Urllib3 Documentation create_connection is an attribute from socks. Urllib3文档中所述, create_connection是socks的一个属性。

Urllib3 version: 1.19.1 Urllib3版本:1.19.1

Requests version: 2.12.4 请求版本:2.12.4

PySocks version: 1.6.5 PySocks版本:1.6.5

import requests

session = requests.Session()
session.proxies = {'http':'socks5://127.0.0.1:9050'}
response = session.get('http://www.icanhazip.com', timeout=2)
print(response.text)

edit: 编辑:

Stacktrace: 堆栈跟踪:

    Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 501, in get
    return self.request('GET', url, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/adapters.py", line 423, in send
    timeout=timeout
  File "/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connectionpool.py", line 594, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connectionpool.py", line 361, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.5/http/client.py", line 1106, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.5/http/client.py", line 1151, in _send_request
    self.endheaders(body)
  File "/usr/lib/python3.5/http/client.py", line 1102, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python3.5/http/client.py", line 934, in _send_output
    self.send(msg)
  File "/usr/lib/python3.5/http/client.py", line 877, in send
    self.connect()
  File "/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connection.py", line 163, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/contrib/socks.py", line 79, in _new_conn
    conn = socks.create_connection(
AttributeError: module 'socks' has no attribute 'create_connection'

It is because your python instance can't find the required socks libraries. 这是因为你的python实例找不到所需的socks库。

The libraries in /usr/lib/python* are installed via sudo apt-get install python3-requests etc. /usr/lib/python*中的库是通过sudo apt-get install python3-requestssudo apt-get install python3-requests

However apt-get can't install the required pysocks libraries that the urllib3 uses. 但是,apt-get无法安装urllib3使用的必需pysocks库。 sudo apt-get install python3-socks won't work. sudo apt-get install python3-socks无效。

What will work is if you install it via sudo -H pip3 install pysocks urllib3[socks] requests[socks] 如果你通过sudo -H pip3 install pysocks urllib3[socks] requests[socks]安装它将会起作用

This will install it under here sudo ls -lrtah /usr/local/lib/python*/dist-packages/ 这将安装在这里sudo ls -lrtah /usr/local/lib/python*/dist-packages/

Make sure you terminal session has /usr/lib:/usr/local/lib in the system path. 确保终端会话在系统路径中包含/usr/lib:/usr/local/lib If your python instance still can't find your socks library, then make sure both paths are part of your ~/.bashrc PYTHON_PATH export. 如果您的python实例仍然找不到您的socks库,那么请确保两个路径都是〜/ .bashrc PYTHON_PATH导出的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM