81

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.

3
  • Does adding proxies=None to the request work?
    – Lesleh
    Feb 14, 2015 at 23:50
  • 3
    @Lesleh that is the default for the proxies keyword argument, which means it won't change the behavior at all.
    – Lukas Graf
    Feb 14, 2015 at 23:58
  • @Lesleh I tried that and unfortunately, it does not work. :(
    – t777
    Feb 14, 2015 at 23:59

8 Answers 8

150

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

  • Create a session
  • Set session.trust_env to False
  • Create your request using that session
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:

  • Authentication information from .netrc (code)
  • CA bundles defined in REQUESTS_CA_BUNDLE or CURL_CA_BUNDLE (code)

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')
8
  • Has trust_env=False any other (side-)effects than disabling the proxy?
    – t777
    Feb 15, 2015 at 0:45
  • 11
    Actually, on one of my servers, no_proxy is the correct answer (lower case). Nov 17, 2015 at 13:33
  • another trick is to let urllib.getproxies return a not empty dict (urllib.getproxies=lambda: {'z':'z'}). then requests will not get proxy setting from the env and os settings.
    – cfy
    Nov 30, 2015 at 2:33
  • 3
    os.environ['NO_PROXY'] = os.environ['NO_PROXY'] + '\,'+ 'stackoverflow.com' So as to not to replace your default proxies, appending to list will do
    – LeDerp
    Jan 29, 2019 at 15:44
  • trust_env = False solution worked perfectly, thanks for the solution! Oct 22, 2020 at 10:02
89

You can choose proxies for each request. From the docs:

import requests

proxies = {
  "http": "http://10.10.1.10:3128",
  "https": "http://10.10.1.10:1080",
}

requests.get("http://example.org", proxies=proxies)

So to disable the proxy, just set each one to the empty string:

import requests

proxies = {
  "http": "",
  "https": "",
}

requests.get("http://example.org", proxies=proxies)

Update: Switched from None to "", see comments.

8
  • 2
    Did you have check for 2nd case. In my case it's not working i.e. it's still using system proxy.
    – sonus21
    Feb 23, 2016 at 6:30
  • @sonukumar What do you mean by "2nd case?"
    – jtpereyda
    Feb 24, 2016 at 18:01
  • 2
    If you are just using HTTP then you don't need to set "https": None, for those wondering
    – 2rs2ts
    May 18, 2017 at 0:29
  • 3
    I recommend against specifying None, check my answer.
    – xjcl
    Jun 8, 2022 at 21:53
  • 4
    Hi, I'm coming from 2022 and None no longer works, instead it has to be an empty string.
    – thethiny
    Sep 21, 2022 at 15:58
19

The way to stop requests/urllib from proxying any requests is to set the the no_proxy (or NO_PROXY) environment variable to * e.g. in bash:

export no_proxy='*'

Or from Python:

import os
os.environ['no_proxy'] = '*' 

To understand why this works is because the urllib.request.getproxies function first checks for any proxies set in the environment variables (e.g. http_proxy, HTTP_PROXY, https_proxy, HTTPS_PROXY, etc) or if none are set then it will check for system configured proxies using platform specific calls (e.g. On MacOS it will check using the system scutil/configd interfaces, and on Windows it will check the Registry). As mentioned in the comments if any proxy variables are set you can reset them as @udani suggested, or unset them like this from Python:

del os.environ['HTTP_PROXY']

Then when urllib attempts to use any proxies the proxyHandler function it will check for the presence and setting of the no_proxy environment variable - which can either be set to specific hostnames as mentioned above or it can be set the special * value whereby all hosts bypass the proxy.

2
  • Using * didn't work for me. However, I only had a few exceptions, so listing them explicitly worked great: export no_proxy='site1.com, site2.domain.com, site3.gov.com'
    – Jack G
    May 24, 2021 at 8:07
  • In my case I had a 'HTTP_PROXY' field in the environment variables. Setting the value as os.environ['HTTP_PROXY'] = '-' fixed it.
    – udani
    Feb 9, 2022 at 19:40
8

With Python3, jtpereyda's solution didn't work, but the following did:

proxies = {
    "http": "",
    "https": "",
}
5

requests library respects environment variables. http://docs.python-requests.org/en/latest/user/advanced/#proxies

So try deleting environment variables HTTP_PROXY and HTTPS_PROXY.

import os
for k in list(os.environ.keys()):
    if k.lower().endswith('_proxy'):
        del os.environ[k]
1
  • 2
    It definitely does not check the environment variables on a Mac.
    – Yongwei Wu
    Jan 4, 2017 at 9:04
4

I implemented @jtpereyda's solution in our production codebase which worked fine on normal successful HTTP requests (200 OK), but this code ended up not working when receiving an HTTP redirect (301 Moved Permamently). Instead use:

requests.get("https://pypi.org/pypi/pillow/9.0.0/json", proxies={"http": "", "https": ""})

For comparison, this line causes a requests.exception.SSLError when behind a proxy (pypi.org tries to redirect us to Pillow with an uppercase P):

requests.get("https://pypi.org/pypi/pillow/9.0.0/json", proxies={"http": None, "https": None})
1
3
 r = requests.post('https://localhost:44336/api/',data='',verify=False)

I faced the same issue when connecting with localhost to access my .net backend from a Python script with the request module.

I set verify to False, which cancels the default SSL verification.

P.s - above code will throw a warning that can be neglected by below one

import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
r=requests.post('https://localhost:44336/api/',data='',verify=False)
0

For those for which no_proxy="*" doesnt work, try 0.0.0.0/32, that worked for me.

2
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jul 27, 2022 at 2:32
  • 2
    Thanks Henning, Do you mean adding 0.0.0.0/32 like this? client.transport.session.proxies = { # Utilize for all http/https connections 'http': "0.0.0.0/32", 'https': "0.0.0.0/32", 'no_proxy': "*", }
    – Brijesh
    Jul 27, 2022 at 11:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.