Cannot connect to proxy error on requests.get() or requests.post() in python

放肆的年华 提交于 2021-02-18 16:55:34

问题


I have two URLs to fetch data from. Using my code, the first URL is working, whereas the second URL is giving ProxyError.

I am using requests library in Python 3 and tried searching the problem in Google and here, but with no success.

My code snippet is:

    import requests

    proxies = {
      'http': 'http://user:pass@xxx.xxx.xxx.xxx:xxxx',
      'https': 'http://user:pass@xxx.xxx.xxx.xxx:xxxx',
    }

    url1 = 'https://en.oxforddictionaries.com/definition/act'
    url2 = 'https://dictionary.cambridge.org/dictionary/english/act'

    r1 = requests.get(url1, proxies=proxies)
    r2 = requests.get(url2, proxies=proxies)

url1 works fine, but url2 gives following error:

    ProxyError: HTTPSConnectionPool(host='dictionary.cambridge.org', port=443): Max retries exceeded with url: /dictionary/english/act (Caused by ProxyError('Cannot connect to proxy.', RemoteDisconnected('Remote end closed connection without response',)))

Same happens on using request.post()

  1. Please explain me why this is happening, and is there any difference between the handshaking of both the URLs?

  2. urllib.request.urlopen is working fine, so I am explicity looking for answers using requests


回答1:


I was able to illicit a valid response for url2 when using headers keyword argument with User-Agent string set to Chrome.

r2 = requests.get(url2, proxies=proxies, headers={'User-Agent': 'Chrome'})

To answer your first question, possible reason for this happening is related to server-side settings. It might be configured not to accept requests originating from unknown agents or requests with a missing User-Agent header.



来源:https://stackoverflow.com/questions/54131794/cannot-connect-to-proxy-error-on-requests-get-or-requests-post-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!