从终端使用 SOCKS5 时 Python 的请求 "Missing dependencies for SOCKS support"

Python's requests "Missing dependencies for SOCKS support" when using SOCKS5 from Terminal

我正在尝试使用依赖于 Python 请求的包与 Python 2.7 shell 中的 API 进行交互。事情是远程地址被我的网络(大学图书馆)阻止了。

所以说 API 我做了以下事情:

~$ ssh -D 8080 name@myserver.com

然后,在新终端中,在本地计算机中:

~$ export http_proxy=socks5://127.0.0.1:8080 https_proxy=socks5://127.0.0.1:8080

然后我 运行 Python 控制台中的程序但是失败了:

~$ python
>>> import myscript
>>> id = '1213'
>>> token = 'jd87jd9'
>>> connect(id,token)

File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 518, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 585, in send
    r = adapter.send(request, **kwargs)
  File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 370, in send
    conn = self.get_connection(request.url, proxies)
  File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 273, in get_connection
    proxy_manager = self.proxy_manager_for(proxy)
  File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 169, in proxy_manager_for
    **proxy_kwargs
  File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 43, in SOCKSProxyManager
    raise InvalidSchema("Missing dependencies for SOCKS support.")
requests.exceptions.InvalidSchema: Missing dependencies for SOCKS support.

此节选自 adapters.py requests module:

> try:
>     from .packages.urllib3.contrib.socks import SOCKSProxyManager except ImportError:
>     def SOCKSProxyManager(*args, **kwargs):
>         raise InvalidSchema("Missing dependencies for SOCKS support.")

现在问题似乎出在 urllib3 的 SOCKSProxyManager。

所以我读到如果你安装了 PySocks 或者你做了 pip install urllib3[socks]

,你可以将 SOCKSProxyManager 与 SOCKS5 一起使用

唉,我用 Socks 尝试了 PySocks 和 urllib3,但都没有成功。

有其他解决方法吗?

编辑:

我也尝试了 pip install requests[socks](即支持 Socks 的请求 2.10.0),我得到了这个:

  File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 467, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: SOCKSHTTPSConnectionPool(host='api-server.com', port=443): Max retries exceeded with url: /auth (Caused by NewConnectionError('<requests.packages.urllib3.contrib.socks.SOCKSHTTPSConnection object at 0x95c7ccc>: Failed to establish a new connection: SOCKS5 proxy server sent invalid data',))

我将 requests[socks]>=2.10.0 添加到我的 requirements.txt,更新了我的 https_proxy 环境变量,并遇到了上述错误。然后我在重置 https_proxy env 变量并安装 PySocks 后尝试了常规 pip install requests[socks] 。我不确定为什么 pip install -Ur requirements.txt 第一次安装 PySocks 失败。

之后,我可以使用 socks 代理在 python 中发出请求。

您的 socks 服务器似乎不正常。我会看看您或您的管理员是否可以查看日志并查看机器在抱怨什么。

我在使用 conda 和请求 2.11 时遇到了同样的问题(我在公司代理后面的 Ubuntu VM 中工作)。

This issue 帮助了我。我将环境变量 all_proxy(最初设置为 SOCK 代理 socks://....)更改为 .bashrc 文件中的 https 版本:

export all_proxy="https://<proxy>:<port>/"

现在可以使用了。

只需取消设置您的 all_proxy 环境变量,这应该会起作用。也可以参考github中的这个issue

在 Ubuntu 上,您可以使用以下命令 unset all_proxy 并重新启动终端

我在做一个简单的 pip install -U pip 时也偶然发现了这个问题,但是我从你的问题中找到的信息帮助我解决了我的问题。我在 Mac OS X.

正如您所指出的,requests 包中的 adapters.py 正在尝试这样做:

try:
    from .packages.urllib3.contrib.socks import SOCKSProxyManager
except ImportError:
    def SOCKSProxyManager(*args, **kwargs):
        raise InvalidSchema("Missing dependencies for SOCKS support.")

所以寻找 place of definition of SOCKSProxyManager 似乎是明智的。它似乎在 urllib3 的 "contrib" 模块中,默认情况下没有与 urllib3 一起安装。该模块的文档字符串说:

This module contains provisional support for SOCKS proxies from within
urllib3. This module supports SOCKS4 (specifically the SOCKS4A variant) and
SOCKS5. To enable its functionality, either install PySocks or install this
module with the ``socks`` extra.

pip docs 的说明说明了有关 setuptools extras 的内容:

6. Install a package with setuptools extras.

$ pip install SomePackage[PDF]
$ pip install git+https://git.repo/some_pkg.git#egg=SomePackage[PDF]
$ pip install SomePackage[PDF]==3.0
$ pip install -e .[PDF]==3.0  # editable project in current directory

所以我按照说明做了:

$ pip install 'urllib3[socks]'

然后我继续pip install -U pip,这是我应该做的,现在它起作用了。

我想知道有多少人被方括号骗了,因为 Bash 和其他 shell 经常把它当作一个特殊字符,需要对其进行转义才能到达调用的程序(在这种情况下,pip)。

这意味着 requests 正在使用 socks 作为代理,但未安装 socks。

就运行 pip install pysocks

几分钟后我遇到了同样的错误 ago.Then 通过 pip 重新安装了 request[socks]。 袜子好像少了一部分win-inet_pton。 pip安装,问题解决

我的环境是 Ubuntu 16.4 LTS 和 Python3.5.2 我使用 pip3 安装 libs 遇到了同样的问题。所以我使用命令 unset ALL_PROXY 来解决这个问题并且它有效。

PS: 使用 printenv | grep -i proxy 显示代理信息。

在 Ubuntu 你可以 运行 :
unset all_proxy && unset ALL_PROXY

在 ubuntu 中,我执行以下命令:

# Unset socks proxy
unset all_proxy    
unset ALL_PROXY
# Install missing dependencies:
pip install pysocks
# Reset proxy
source ~/.bashrc

我使用了以上所有方法,但只有这个对我有用:

set | grep -i all_proxy

它返回:

ALL_PROXY=socks://127.0.0.1:1080/
all_proxy=socks://127.0.0.1:1080/

然后,我这样做了:

export all_proxy=""

终于没有再出现错误了

在 debian 10 中,Python 3:

pip3 install pysocks

我遇到了同样的问题,在 Anaconda Python 3.7

中出现错误 Missing dependencies for SOCKS support.

由于 conda 命令工作正常,我使用命令 conda install pysocks

安装了 pysocks

这解决了问题。

您必须使用 python requests 库添加袜子支持:

pip install requests[socks]

在报告 pysocks 不会被 requests[socks]>=0.23.0 要求拉动的问题后出现相同的错误。

我可以(深奥地)通过删除我在相同 requirements.txt.

中也有的 requests>=0.23.0 要求来修复它

运行 Python3.7 在 Debian 上,报告错误的用户是 运行 python3.6 在 Ubuntu.

补丁前

$ cat requirements.txt 
requests>=2.23.0
requests[socks]>=2.23.0
$ python3 -m venv venv
$ . venv/bin/activate
(venv) $ pip --no-cache-dir install -r requirements.txt
Collecting requests>=2.23.0 (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/45/1e/0c169c6a5381e241ba7404532c16a21d86ab872c9bed8bdcd4c423954103/requests-2.24.0-py2.py3-none-any.whl (61kB)
    100% |████████████████████████████████| 71kB 202kB/s 
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 (from requests>=2.23.0->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/9f/f0/a391d1463ebb1b233795cabfc0ef38d3db4442339de68f847026199e69d7/urllib3-1.25.10-py2.py3-none-any.whl (127kB)
    100% |████████████████████████████████| 133kB 617kB/s 
Collecting idna<3,>=2.5 (from requests>=2.23.0->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/a2/38/928ddce2273eaa564f6f50de919327bf3a00f091b5baba8dfa9460f3a8a8/idna-2.10-py2.py3-none-any.whl (58kB)
    100% |████████████████████████████████| 61kB 2.8MB/s 
Collecting chardet<4,>=3.0.2 (from requests>=2.23.0->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 10.4MB/s 
Collecting certifi>=2017.4.17 (from requests>=2.23.0->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/5e/c4/6c4fe722df5343c33226f0b4e0bb042e4dc13483228b4718baf286f86d87/certifi-2020.6.20-py2.py3-none-any.whl (156kB)
    100% |████████████████████████████████| 163kB 4.7MB/s 
Installing collected packages: urllib3, idna, chardet, certifi, requests
Successfully installed certifi-2020.6.20 chardet-3.0.4 idna-2.10 requests-2.24.0 urllib3-1.25.10

补丁后

现在应用以下补丁:

diff --git a/sauron/requirements.txt b/sauron/requirements.txt
index a0c901b..f38d18f 100644
--- a/sauron/requirements.txt
+++ b/sauron/requirements.txt
@@ -1,2 +1 @@
-requests>=2.23.0
 requests[socks]>=2.23.0
$ cat requirements.txt 
requests[socks]>=2.23.0
$ cat requirements.txt 
requests[socks]>=2.23.0
(venv) $ pip --no-cache-dir install -r requirements.txt 
Collecting requests[socks]>=2.23.0 (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/45/1e/0c169c6a5381e241ba7404532c16a21d86ab872c9bed8bdcd4c423954103/requests-2.24.0-py2.py3-none-any.whl (61kB)
    100% |████████████████████████████████| 71kB 234kB/s 
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 (from requests[socks]>=2.23.0->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/9f/f0/a391d1463ebb1b233795cabfc0ef38d3db4442339de68f847026199e69d7/urllib3-1.25.10-py2.py3-none-any.whl (127kB)
    100% |████████████████████████████████| 133kB 678kB/s 
Collecting chardet<4,>=3.0.2 (from requests[socks]>=2.23.0->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 2.3MB/s 
Collecting idna<3,>=2.5 (from requests[socks]>=2.23.0->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/a2/38/928ddce2273eaa564f6f50de919327bf3a00f091b5baba8dfa9460f3a8a8/idna-2.10-py2.py3-none-any.whl (58kB)
    100% |████████████████████████████████| 61kB 6.2MB/s 
Collecting certifi>=2017.4.17 (from requests[socks]>=2.23.0->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/5e/c4/6c4fe722df5343c33226f0b4e0bb042e4dc13483228b4718baf286f86d87/certifi-2020.6.20-py2.py3-none-any.whl (156kB)
    100% |████████████████████████████████| 163kB 4.8MB/s 
Collecting PySocks!=1.5.7,>=1.5.6; extra == "socks" (from requests[socks]>=2.23.0->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/8d/59/b4572118e098ac8e46e399a1dd0f2d85403ce8bbaad9ec79373ed6badaf9/PySocks-1.7.1-py3-none-any.whl
Installing collected packages: urllib3, chardet, idna, certifi, PySocks, requests
Successfully installed PySocks-1.7.1 certifi-2020.6.20 chardet-3.0.4 idna-2.10 requests-2.24.0 urllib3-1.25.10

Pysocks 被有效拉动。

我的修复 is:After 我取消代理我的终端,安装 运行 很好。

使用 apt-get 代替 pip 怎么样?
当我使用 apt-get 时,我可以忽略代理设置,所以下面的方法对我很有效。

正在使用

apt-get update && apt-get install  python-socks

而不是

pip install pysocks