pythonpython-requests

requests: how to disable / bypass proxy


I am getting an url with:

r = requests.get("http://myserver.com")

As I can see in the 'access.log' of "myserver.com", the client's system proxy is used. But I want to disable using proxies at all with requests.


Solution

  • The only way I'm currently aware of for disabling proxies entirely is the following:

    import requests
    
    session = requests.Session()
    session.trust_env = False
    
    response = session.get('http://www.stackoverflow.com')
    

    This is based on this comment by Lukasa and the (limited) documentation for requests.Session.trust_env.

    Note: Setting trust_env to False also ignores the following:


    If however you only want to disable proxies for a particular domain (like localhost), you can use the NO_PROXY environment variable:

    import os
    import requests
    
    os.environ['NO_PROXY'] = 'stackoverflow.com'
    
    response = requests.get('http://www.stackoverflow.com')