Python:HTTPS使用具有请求的代理服务器

时间:2018-11-15 01:27:41

标签: python https proxy python-requests

我正在使用MyIPHide。我下载了他们的客户端软件,进行了安装并打开了服务。

我可以使用浏览器正常访问https个网站,但不能使用requests来获取页面

这有效:

import requests
IP=requests.get('http://api.ipify.org').text

proxyDict = { "http"  : IP,
              "https"  : IP
            }

url='http://www.cnn.com'
r=requests.get(url,proxies=proxyDict)

这不是:

url='https://www.cnn.com'
r=requests.get(url,proxies=proxyDict)

只有httphttps的差异

这是回溯:

File "C:\Python27\lib\site-packages\requests\adapters.py", line 502, in send
    raise ProxyError(e, request=request)
ProxyError: HTTPSConnectionPool(host='www.cnn.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', error(10053, 'An established connection was aborted by the software in your host machine')))

我尝试了其他https个网站,但它们都无法正常工作。

我还通过MyIPHide通过电子邮件发送了支持。他们说所有代理都支持https,当我仅使用浏览器时,情况就是如此。

一种可行的解决方法是,如果我使用Selenium并获取页面,然后使用driver.page_source作为文本。

这不是代理服务器问题,因为我已经通过sslprivateproxy.com购买了私有代理服务器地址,并输入了IP和端口,但仍然遇到相同的错误。

我正在使用Python 2.7.15,并要求2.20.1。非代理使用请求有效,即:

import requests
url='https://www.cnn.com'
r=requests.get(url)

>>> r
<Response [200]>
>>>

还尝试了python 3.6的请求2.20.1->相同的结果。

1 个答案:

答案 0 :(得分:1)

On根据PoolManager的大小请求它,因此,如果您想在池中拥有更多连接,可以重置HTTPAdapter()的大小。这样的方式(为确保它可以正常工作,可以将其设置为0)。

import requests
from requests.adapters import HTTPAdapter

proxy = {"http":"118.174.233.31:51726",
        "https":"43.254.132.86:50659"} # free proxy,often need to modify

with requests.Session() as se:
    # pool_connections=pool_maxsize=0 -> pool closed
    se.mount('https://', HTTPAdapter(pool_connections=100,pool_maxsize=100)) 
    se.mount('http://', HTTPAdapter(pool_connections=100,pool_maxsize=100))
    print(se.adapters["https://"]._pool_maxsize)
    #print(se.get("https://github.com"))
    print(se.get('http://api.ipify.org',proxies=proxy).text)

#100
#118.174.233.31
相关问题