Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 750
t777 Avatar asked Feb 14 '15 23:02

t777


People also ask

What is my bypass proxy for?

The Proxy Bypass tab of the Bypass Settings page enables you to define sites that bypass the cloud service for all policies. This may include, for example, internal sites that are not accessible from the Internet, so the cloud service cannot serve or analyze them.


2 Answers

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') 
like image 120
Lukas Graf Avatar answered Sep 18 '22 22:09

Lukas Graf


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 None:

import requests  proxies = {   "http": None,   "https": None, }  requests.get("http://example.org", proxies=proxies) 
like image 42
jtpereyda Avatar answered Sep 18 '22 22:09

jtpereyda