requests.exceptions.ConnectionError:
SOCKSHTTPConnectionPool(host='myhost', port=80):
Max retries exceeded with url: /my/path
(Caused by NewConnectionError('<requests.packages.urllib3.contrib.socks.SOCKSConnection object at 0x106812bd0>:
Failed to establish a new connection:
[Errno 8] nodename nor servname provided, or not known',))
export https_proxy=socks5://<hostname or ip>:<port>
1.运行脚本。此示例使用代理发出请求并显示IP地址:
echo Your real IP
python -c 'import requests;print(requests.get("http://ipinfo.io/ip").text)'
echo IP with socks-proxy
python -c 'import requests;print(requests.get("https://ipinfo.io/ip").text)'
import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, "127.0.0.1", 1080)
def create_connection(address, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
source_address=None, socket_options=None):
"""Connect to *address* and return the socket object.
Convenience function. Connect to *address* (a 2-tuple ``(host,
port)``) and return the socket object. Passing the optional
*timeout* parameter will set the timeout on the socket instance
before attempting to connect. If no *timeout* is supplied, the
global default timeout setting returned by :func:`getdefaulttimeout`
is used. If *source_address* is set it must be a tuple of (host, port)
for the socket to bind as a source address before making the connection.
An host of '' or port 0 tells the OS to use the default.
"""
host, port = address
if host.startswith('['):
host = host.strip('[]')
err = None
for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
sock = None
try:
sock = socks.socksocket(af, socktype, proto)
# If provided, set socket level options before connecting.
# This is the only addition urllib3 makes to this function.
urllib3.util.connection._set_socket_options(sock, socket_options)
if timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
sock.settimeout(timeout)
if source_address:
sock.bind(source_address)
sock.connect(sa)
return sock
except socket.error as e:
err = e
if sock is not None:
sock.close()
sock = None
if err is not None:
raise err
raise socket.error("getaddrinfo returns an empty list")
# monkeypatch
urllib3.util.connection.create_connection = create_connection
9条答案
按热度按时间bqf10yzr1#
现代道:
然后
dojqjjoe2#
如果有人尝试了所有这些旧的答案,仍然遇到这样的问题:
这可能是因为,默认情况下,
requests
配置为在连接的 * 本地 * 端解析DNS查询。尝试将您的代理URL从
socks5://proxyhost:1234
更改为socks5h://proxyhost:1234
。请注意额外的h
(它代表主机名解析)。PySocks包模块默认是进行远程解析,我不知道为什么请求使它们的集成变得如此模糊,但我们在这里。
00jrzges3#
从2016年4月29日发布的
requests
版本2.10.0开始,requests
支持SOCKS。它需要PySocks,可以与
pip install pysocks
一起安装。示例用法:
3qpi33ja4#
您需要安装pysocks,我的版本是1.0,代码对我有效:
xv8emn3q5#
一旦python
requests
与SOCKS5
pull请求合并,它将像使用proxies
字典一样简单:更新:PR已合并。
请参阅SOCKS Proxy Support
另一个选择是,如果你不能等待
request
准备好,当你不能使用requesocks
- like在GoogleAppEngine上,由于缺乏pwd
内置模块,是使用PySocks,上面提到的:1.从存储库中获取
socks.py
文件,并将副本放在根文件夹中;1.将
import socks
和import socket
相加此时,请在使用
urllib2
之前配置并绑定套接字-在以下示例中:cclgggtu6#
您可以使用
https_proxy
环境变量运行脚本。1.必要时安装保护套支架。
1.安装程序环境变量
1.运行脚本。此示例使用代理发出请求并显示IP地址:
qmelpv7a7#
mftmpeh88#
我在urllib3中安装了pysocks和monkey补丁create_connection,如下所示:
gupuwyp29#
我可以在Linux上做这个。