当使用来自终端的SOCKS5时,Python的请求“缺少对SOCKS支持的依赖性”

时间:2016-08-05 16:35:57

标签: python-2.7 python-requests socks urllib3

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

所以说到API,我会做以下事情:

$("#accordion").accordion({
  active: 0,
  heightStyle: "content",
  collapsible: true
});
$("input:radio, input:checkbox").checkboxradio({
  icon: false
});
$(".controle").controlgroup();

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

~$ ssh -D 8080 name@myserver.com

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

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

此摘录来自adapters.py requests module

~$ 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.

现在问题似乎源于urllib3的SOCKSProxyManager。

所以我读到你可以使用带有SOCKS5的SOCKSProxyManager,如果你已经安装了PySocks,或者你做了 pip install urllib3 [socks]

唉,我用袜子试过PySocks和urllib3都没有成功。

还有其他解决方法吗?

修改

我还尝试了 pip安装请求[socks] (那是带有Socks支持的请求2.10.0),我得到了这个:

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

14 个答案:

答案 0 :(得分:71)

我遇到了与conda相同的问题并请求2.11(我在公司代理后面的Ubuntu VM中工作)。

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

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

现在可行。

答案 1 :(得分:42)

这意味着请求使用socks作为代理,并且没有安装socks。

跑吧 pip install pysocks

答案 2 :(得分:11)

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

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

看起来你的socks服务器没有表现。我会看看你或你的管理员是否可以看日志,看看机器在抱怨什么。

答案 3 :(得分:5)

我在做一个简单的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似乎是明智的。它似乎出现在&#34; contrib&#34; urllib3中的模块,默认情况下不与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)。

答案 4 :(得分:5)

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

答案 5 :(得分:5)

在debian 10中,Python 3:

pip3 install pysocks

答案 6 :(得分:2)

我在几分钟前得到了同样的错误。然后通过pip重新安装了 request [socks] 。 似乎缺少袜子的部分 win-inet_pton 。 Pip安装它,问题解决了。

答案 7 :(得分:2)

我遇到了同样的问题,Anaconda Python 3.7出现了错误Missing dependencies for SOCKS support.

由于conda命令工作正常,我用命令pysocks安装了conda install pysocks

这解决了问题。

答案 8 :(得分:1)

刚刚取消设置all_proxy环境变量,这应该可行。您也可以在github中引用此issue

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

答案 9 :(得分:1)

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

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

答案 10 :(得分:1)

我使用了上述所有方法,但只有这种方法对我有用:

set | grep -i all_proxy

它返回:

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

然后,我这样做了:

export all_proxy=""

最后,它没有再出现错误。

答案 11 :(得分:1)

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

pip install requests[socks]

答案 12 :(得分:1)

在报告了pysocks要求不会导致requests[socks]>=0.23.0的问题后,得到了相同的错误。

我可以(完全)通过删除我在同一requests>=0.23.0中也有的requirements.txt要求来解决此问题。

在Debian上运行Python3.7,并且报告该错误的用户正在Ubuntu上运行python3.6。

在补丁之前

$ 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被有效拉了。

答案 13 :(得分:0)

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

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