【问题标题】:Urllib3 socks5 proxy error: 'socks' has no attribute 'create_connection'Urllib3 socks5 代理错误:“socks”没有属性“create_connection”
【发布时间】:2017-01-10 00:46:24
【问题描述】:

我需要 Robobrowser 才能使用 Tor,我认为最简单的方法是:Robobrowser with Sessions。 但是我遇到了一个奇怪的请求问题,或者更具体的 urllib3:

AttributeError: 模块 'socks' 没有属性 'create_connection'

Urllib3 Documentation 中所述,create_connection 是 socks 的一个属性。

Urllib3 版本:1.19.1

请求版本:2.12.4

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)

编辑:

堆栈跟踪:

    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'

【问题讨论】:

  • 为了帮助您,我们都需要有关异常的更多信息。我无法重现您所报告的内容。
  • 您需要什么样的信息,我运行的是 ubuntu 16.04 64 位,我还需要提供什么信息?
  • 完整的堆栈跟踪很有用,即使您编辑路径也是非常必要的。
  • 够了吗?
  • 第一个命令返回AttributeError: module 'socks' has no attribute '__version__' 第二个:&lt;module 'socks' from '/usr/local/lib/python3.5/dist-packages/socks.py'&gt;

标签: python python-requests urllib3 robobrowser


【解决方案1】:

这是因为您的 python 实例找不到所需的 socks 库。

/usr/lib/python* 中的库是通过sudo apt-get install python3-requests 等安装的。

但是 apt-get 无法安装 urllib3 使用的所需 pysocks 库。 sudo apt-get install python3-socks 不起作用。

如果你通过sudo -H pip3 install pysocks urllib3[socks] requests[socks]安装它会起作用

这将在sudo ls -lrtah /usr/local/lib/python*/dist-packages/下安装它

确保您的终端会话在系统路径中有/usr/lib:/usr/local/lib。如果您的 python 实例仍然找不到您的 socks 库,请确保这两个路径都是您的 ~/.bashrc PYTHON_PATH 导出的一部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多