Python 请求不适用于 https 代理 [英] Python Requests doesnt work for https proxy
问题描述
我尝试在 python 中使用 https 代理,如下所示:
I try to use https proxy in python like this:
proxiesDict ={
'http': 'http://' + proxy_line,
'https': 'https://' + proxy_line
}
response = requests.get('https://api.ipify.org/?format=json', proxies=proxiesDict, allow_redirects=False)
proxy_line
是从文件中读取的代理,格式为 ip:port.我在浏览器中检查了这个 https 代理,它工作正常.但是在 python 中,这段代码挂了几秒钟,然后我得到了异常:
proxy_line
is a proxy read from file in the format of ip:port. I checked this https proxy in browser and it works. But in python this code hangs for a few seconds and then i get exception:
HTTPSConnectionPool(host='api.ipify.org', port=443): Max retries exceeded with url: /?format=json (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0425E450>: Failed to establish a new connection: [WinError 10060]
我尝试使用socks5代理,它适用于安装了PySocks的socks5代理.但是对于 https 我得到这个例外,有人可以帮我
I tried to use socks5 proxy, and it works on socks5 proxies with a PySocks installed. But for https i get this exception, can someone help me
推荐答案
为requests
指定代理列表时,key是协议,value是domain/ip.对于实际值,您无需再次指定 http://
或 https://
.
When specifying a proxy list for requests
, the key is the protocol, and the value is the domain/ip. You don't need to specify http://
or https://
again, for the actual value.
因此,您的 proxiesDict
将是:
proxiesDict = {
'http': proxy_line,
'https': proxy_line
}
这篇关于Python 请求不适用于 https 代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!