Using requests behind a proxy
Setting proxy in Python code
If your code is running behind a proxy and you know the end point, you can set this information in your code.
requests
accepts a proxies
parameter. This should be a dictionary that maps protocol to the proxy URL.
proxies = {
'http': 'https://proxy.example.com:8080',
'https': 'https://secureproxy.example.com:8090',
}
Notice that in the dictionary we have defined the proxy URL for two separate protocols: HTTP and HTTPS. Each maps to an individual URL and port. This does not mean that the two can’t be the same, though. This is also acceptable:
proxies = {
'http': 'https://secureproxy.example.com:8090',
'https': 'https://secureproxy.example.com:8090',
}
Once your dictionary is defined, you pass it as a parameter.
requests.get('https://example.org', proxies=proxies)
Using proxy environment variables
requests
uses specific environment variables automatically for proxy detection.
HTTP_PROXY
will define the proxy URL to use for HTTP connectionsHTTPS_PROXY
will define the proxy URL to use for HTTPS connections
Once these environment variables are set, the Python code does not need to pass anything to the proxies
parameter.
requests.get('https://example.com')