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

时间:2022-08-22 18:10:31

I'm trying to interact with an API from my Python 2.7 shell using a package that relies on Python's requests. Thing is the remote address is blocked by my network (university library).

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

So to speak to the API I do the following:

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

~$ ssh -D 8080 name@myserver.com

And then, in new terminal, in local computer:

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

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

Then I run the program in Python console but fails:

然后我在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.

This excerpt is from the adapters.py requests module:

这段节选自适配器。py请求模块:

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

Now problem seems to be originated in urllib3's SOCKSProxyManager.

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

So I read you can use SOCKSProxyManager with SOCKS5 if you have install PySocks or you do a pip install urllib3[socks]

所以我读了你可以用SOCKSProxyManager和SOCKS5如果你安装了PySocks或者你做了pip安装urllib3[袜子]

Alas, I tried both PySocks and urllib3 with Socks without any success.

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

Any idea of another workaround?

有什么办法吗?

EDIT:

编辑:

I also tried pip install requests[socks] (that's requests 2.10.0 with Socks support) and I am getting this:

我还尝试了pip安装请求[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',))

8 个解决方案

#1


61  

I had the same issue with conda and requests 2.11 (I work in a Ubuntu VM behind a corporate proxy).

对于conda和请求2.11,我也有同样的问题(我在一个公司代理后面的Ubuntu VM里工作)。

This issue helped me. I changed my environment variable all_proxy (which was originally set to a SOCK proxy socks://....) to the https version in my .bashrc file :

这个问题帮助了我。我改变了环境变量all_proxy(最初设置为一个袜子代理袜子:/ / ....)到https版本在我. bashrc文件:

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

出口all_proxy = " https:// <代理> : <口> /”

and now it works.

现在它的工作原理。

#2


13  

This means that requests is using socks as a proxy and that socks is not installed.

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

Just run pip install pysocks

运行pip安装pysocks

#3


10  

I added the requests[socks]>=2.10.0 to my requirements.txt, updated my https_proxy env variable, and came across the above error. I then tried a regular pip install requests[socks] after resetting the https_proxy env variable and PySocks was installed. I'm not sure why the pip install -Ur requirements.txt failed to install PySocks the first time.

我将请求[socks]>=2.10.0添加到我的需求中。txt,更新了我的https_proxy env变量,并遇到了上面的错误。然后,在重新设置https_proxy env变量并安装PySocks之后,我尝试了一个常规的pip安装请求[socks]。我不确定为什么pip安装-Ur需求。txt在第一次安装PySocks失败。

After that, I was able to make a request in python using the socks proxy.

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

It looks like your socks server is not behaving. I would see if you, or your admin, could watch the logs and see what the machine is complaining about.

看起来您的socks服务器没有行为。我想看看你,或者你的管理员,能不能看一下日志,看看机器在抱怨什么。

#4


2  

Just unset your all_proxy environment variable, and this should work. Also you can refer to this issue in github.

只要取消all_proxy环境变量的设置,就可以了。你也可以在github上参考这个问题。

On Ubuntu you can use the following command unset all_proxy and restart terminal

在Ubuntu上,您可以使用以下命令unset all_proxy和restart terminal

#5


2  

I also stumbled upon this issue while doing a simple pip install -U pip, but information I found from your question helped me resolved my issue. I am on Mac OS X.

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

As you have pointed out, adapters.py from the requests package was trying to do this:

正如您所指出的,适配器。来自request package的py试图这样做:

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

So it seems sensible to look for the place of definition of SOCKSProxyManager. It seems to be in a "contrib" module in urllib3 and not installed alongside urllib3 by default. The docstring of that module says:

因此,寻找SOCKSProxyManager定义的位置似乎是明智的。它似乎是在urllib3的“悔过”模块中,而不是默认安装在urllib3旁边。该模块的docstring表示:

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.

The instructions of the pip docs says this about setuptools extras:

pip文档的说明是关于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

So I followed the instructions and did:

所以我按照指示做了:

$ pip install 'urllib3[socks]'

I then continued with pip install -U pip, which was what I was supposed to be doing, and now it works.

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

I wonder how many people got tricked-up by the square brackets, as Bash and other shells often treat it as a special character, which needs to be escaped for it to reach the invoked program (in this case, pip).

我想知道有多少人被方括号欺骗了,因为Bash和其他shell通常将它视为一个特殊字符,它需要转义才能到达被调用的程序(在本例中是pip)。

#6


2  

I got the same error just a few mins ago.Then re-installed request[socks] via pip. It seems that there was a missing part of socks which is win-inet_pton. Pip installed it and the problem is solved.

就在几分钟前,我犯了同样的错误。然后通过pip重新安装请求[socks]。似乎有一件遗失的袜子是win-inet_pton。Pip安装了它,问题就解决了。

#7


2  

In Ubuntu you can run :
unset all_proxy && unset ALL_PROXY

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

#8


1  

My environment is Ubuntu 16.4 LTS and Python3.5.2 I use pip3 to install libs got the same issue. so I use the command unset ALL_PROXY to solve this problem and it works.

我的环境是Ubuntu 16.4 LTS和Python3.5.2我使用pip3安装libs也有同样的问题。因此我使用命令unset ALL_PROXY来解决这个问题,它是有效的。

PS: use printenv | grep -i proxy to show the proxy info.

使用printenv | grep -i代理来显示代理信息。

#1


61  

I had the same issue with conda and requests 2.11 (I work in a Ubuntu VM behind a corporate proxy).

对于conda和请求2.11,我也有同样的问题(我在一个公司代理后面的Ubuntu VM里工作)。

This issue helped me. I changed my environment variable all_proxy (which was originally set to a SOCK proxy socks://....) to the https version in my .bashrc file :

这个问题帮助了我。我改变了环境变量all_proxy(最初设置为一个袜子代理袜子:/ / ....)到https版本在我. bashrc文件:

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

出口all_proxy = " https:// <代理> : <口> /”

and now it works.

现在它的工作原理。

#2


13  

This means that requests is using socks as a proxy and that socks is not installed.

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

Just run pip install pysocks

运行pip安装pysocks

#3


10  

I added the requests[socks]>=2.10.0 to my requirements.txt, updated my https_proxy env variable, and came across the above error. I then tried a regular pip install requests[socks] after resetting the https_proxy env variable and PySocks was installed. I'm not sure why the pip install -Ur requirements.txt failed to install PySocks the first time.

我将请求[socks]>=2.10.0添加到我的需求中。txt,更新了我的https_proxy env变量,并遇到了上面的错误。然后,在重新设置https_proxy env变量并安装PySocks之后,我尝试了一个常规的pip安装请求[socks]。我不确定为什么pip安装-Ur需求。txt在第一次安装PySocks失败。

After that, I was able to make a request in python using the socks proxy.

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

It looks like your socks server is not behaving. I would see if you, or your admin, could watch the logs and see what the machine is complaining about.

看起来您的socks服务器没有行为。我想看看你,或者你的管理员,能不能看一下日志,看看机器在抱怨什么。

#4


2  

Just unset your all_proxy environment variable, and this should work. Also you can refer to this issue in github.

只要取消all_proxy环境变量的设置,就可以了。你也可以在github上参考这个问题。

On Ubuntu you can use the following command unset all_proxy and restart terminal

在Ubuntu上,您可以使用以下命令unset all_proxy和restart terminal

#5


2  

I also stumbled upon this issue while doing a simple pip install -U pip, but information I found from your question helped me resolved my issue. I am on Mac OS X.

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

As you have pointed out, adapters.py from the requests package was trying to do this:

正如您所指出的,适配器。来自request package的py试图这样做:

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

So it seems sensible to look for the place of definition of SOCKSProxyManager. It seems to be in a "contrib" module in urllib3 and not installed alongside urllib3 by default. The docstring of that module says:

因此,寻找SOCKSProxyManager定义的位置似乎是明智的。它似乎是在urllib3的“悔过”模块中,而不是默认安装在urllib3旁边。该模块的docstring表示:

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.

The instructions of the pip docs says this about setuptools extras:

pip文档的说明是关于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

So I followed the instructions and did:

所以我按照指示做了:

$ pip install 'urllib3[socks]'

I then continued with pip install -U pip, which was what I was supposed to be doing, and now it works.

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

I wonder how many people got tricked-up by the square brackets, as Bash and other shells often treat it as a special character, which needs to be escaped for it to reach the invoked program (in this case, pip).

我想知道有多少人被方括号欺骗了,因为Bash和其他shell通常将它视为一个特殊字符,它需要转义才能到达被调用的程序(在本例中是pip)。

#6


2  

I got the same error just a few mins ago.Then re-installed request[socks] via pip. It seems that there was a missing part of socks which is win-inet_pton. Pip installed it and the problem is solved.

就在几分钟前,我犯了同样的错误。然后通过pip重新安装请求[socks]。似乎有一件遗失的袜子是win-inet_pton。Pip安装了它,问题就解决了。

#7


2  

In Ubuntu you can run :
unset all_proxy && unset ALL_PROXY

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

#8


1  

My environment is Ubuntu 16.4 LTS and Python3.5.2 I use pip3 to install libs got the same issue. so I use the command unset ALL_PROXY to solve this problem and it works.

我的环境是Ubuntu 16.4 LTS和Python3.5.2我使用pip3安装libs也有同样的问题。因此我使用命令unset ALL_PROXY来解决这个问题,它是有效的。

PS: use printenv | grep -i proxy to show the proxy info.

使用printenv | grep -i代理来显示代理信息。