python使用requests+代理proxy报错:EOF occurred in violation of protocol (_ssl.c:1123)
在使用python获取别人的网站内容时,使用了requests+proxy代理,结果报错了:HTTPSConnectionPool(host='www.somewebsite.com',port=443):Maxretriesexceededwithurl:/en/products/power.html(CausedbySSLError(SSLEOFError(8,'EOFoccurredinvi
在使用python获取别人的网站内容时,使用了requests+proxy代理,结果报错了:
HTTPSConnectionPool(host='www.somewebsite.com', port=443): Max retries exceeded with url: /en/products/power.html (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1123)')))
原代码是这样的:
proxies = {
"http": "http://{}:{}@{}:{}".format(pconfig['proxyUser'], pconfig['proxyPass'], pconfig['proxyHost'], pconfig['proxyPort']),
"https": "https://{}:{}@{}:{}".format(pconfig['proxyUser'], pconfig['proxyPass'], pconfig['proxyHost'], pconfig['proxyPort'])
}
result = requests.get(url=url, headers=headers, timeout=timeout, proxies=proxies)
不使用代理时,可以直接获取到内容,一使用代理就报错。
我直接拿报错信息,拿去百度一下网上的,各种说法。
比如参考了这个:Python requests.exceptions.SSLError: EOF occurred in violation of protocol | 易学教程
里面的方法是弄一个MyAdapter,重写HTTPAdapter的init_poolmanager函数:
proxies = {
"http": "http://{}:{}@{}:{}".format(pconfig['proxyUser'], pconfig['proxyPass'], pconfig['proxyHost'], pconfig['proxyPort']),
"https": "https://{}:{}@{}:{}".format(pconfig['proxyUser'], pconfig['proxyPass'], pconfig['proxyHost'], pconfig['proxyPort'])
}
s = requests.Session()
urlhost = com_util.getRegOne(r"([a-z][a-z0-9+\-.]*:\/\/[a-z0-9\-._~%]+)", url)
s.mount(urlhost, MyAdapter())
result = s.get(url=url, headers=headers, timeout=timeout, proxies=proxies)
试过了之后,发现没有什么用,仍然报错。
然后换了一种方法搜索这个问题:"requests 代理 报错"
找到了一篇文章:
发现是代理的Key应该大写,然后改了自己的代码:
proxies = {
"HTTP": "http://{}:{}@{}:{}".format(pconfig['proxyUser'], pconfig['proxyPass'], pconfig['proxyHost'], pconfig['proxyPort']),
"HTTPS": "https://{}:{}@{}:{}".format(pconfig['proxyUser'], pconfig['proxyPass'], pconfig['proxyHost'], pconfig['proxyPort'])
}
result = requests.get(url=url, headers=headers, timeout=timeout, proxies=proxies)
终于没有报错了,返回了200状态!
更多推荐
所有评论(0)