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代理,它适用于socks5代理安装了PySocks。但是对于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
推荐答案
为<$ c指定代理列表时$ c> requests ,密钥是协议,值是域/ 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屋!