How to properly use proxies in Python requests?

纵饮孤独 提交于 2021-01-29 20:10:47

问题


I have a list of proxies. When I test one of them using curl:

curl -proxy https://x.x.x:port https://www.google.com/

I get the expected result, but when I run:

proxies = {'http' : 'x.x.x:port', 'https' : 'x.x.x:port'}
requests.get('https://www.google.com/', proxies = proxies)

It is stuck for a while and then I get this error:

requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', ConnectionResetError(104, 'Connection reset by peer')))

I tried different ways of proxy definition, like:

'http' : 'http://x.x.x:port', 'https' : 'https://x.x.x:port'

and nothing works, always get this error, how to fix that?


回答1:


EDIT: Just saw your last note of trying different ways which aligns with my first suggestion. You may want to try setting the proxy with the os library in my later suggestion.

There's two things that stand out to me:

  1. You used https://x.x.x:port in your curl command but used x.x.x:port in Python. You might have to include https://.

  2. You used both http and https. I don't think this one would cause an error, but still worth investigating

First, I would suggest including https:// like so:

proxies = {'https' : 'https://x.x.x:port'}  
requests.get('https://www.google.com/', proxies = proxies) 

Second, I would try just setting https (and if that fails, try just setting http):

import requests

proxies = {
  'https': 'https://x.x.x.x:port',
}

requests.get('https://example.org', proxies=proxies)

Also, you can set environmental proxies through Python like:

import os
os.environ["http_proxy"] = "http://x.x.x.x:port"
os.environ["https_proxy"] = "https://x.x.x.x:port"

or through the command line / command prompt

Windows

set http_proxy=http://x.x.x.x:port
set https_proxy=https://x.x.x.x:port

Linux/OS X:

export http_proxy=http://x.x.x.x:port
export https_proxy=https://x.x.x.x:port


来源:https://stackoverflow.com/questions/61718116/how-to-properly-use-proxies-in-python-requests

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