Skip to content

Instantly share code, notes, and snippets.

@masaki
Created May 21, 2009 12:31
Show Gist options
  • Save masaki/115437 to your computer and use it in GitHub Desktop.
Save masaki/115437 to your computer and use it in GitHub Desktop.
yum enable NO_PROXY (proxy=_none_)
diff --git a/urllib.py b/urllib.py
index 802d9b7..d13ddb7 100644
--- a/urllib.py
+++ b/urllib.py
@@ -35,7 +35,7 @@ __all__ = ["urlopen", "URLopener", "FancyURLopener", "urlretrieve",
"localhost", "thishost", "ftperrors", "basejoin", "unwrap",
"splittype", "splithost", "splituser", "splitpasswd", "splitport",
"splitnport", "splitquery", "splitattr", "splitvalue",
- "splitgophertype", "getproxies"]
+ "splitgophertype", "getproxies", "getnoproxies"]
__version__ = '1.16' # XXX This version is not always updated :-(
@@ -1355,6 +1355,18 @@ else:
def proxy_bypass(host):
return 0
+def getnoproxies_environment():
+ hosts = []
+ if 'no_proxy' in os.environ:
+ hosts = os.environ['no_proxy'].split(",")
+ elif 'NO_PROXY' in os.environ:
+ hosts = os.environ['NO_PROXY'].split(",")
+ hosts = map(lambda host:host.strip(), list(set(hosts)))
+ return hosts
+
+def getnoproxies():
+ return getnoproxies_environment()
+
# Test and time quote() and unquote()
def test1():
s = ''
diff --git a/urllib2.py b/urllib2.py
index 4ac7a39..a37ecb0 100644
--- a/urllib2.py
+++ b/urllib2.py
@@ -118,7 +118,7 @@ from urllib import (unwrap, unquote, splittype, splithost,
splitattr, ftpwrapper, noheaders, splituser, splitpasswd, splitvalue)
# support for FileHandler, proxies via environment variables
-from urllib import localhost, url2pathname, getproxies
+from urllib import localhost, url2pathname, getproxies, getnoproxies
__version__ = "2.4"
@@ -573,6 +573,8 @@ class ProxyHandler(BaseHandler):
lambda r, proxy=url, type=type, meth=self.proxy_open: \
meth(r, proxy, type))
+ self.noproxies = getnoproxies()
+
def proxy_open(self, req, proxy, type):
orig_type = req.get_type()
type, r_type = splittype(proxy)
@@ -585,7 +587,18 @@ class ProxyHandler(BaseHandler):
unquote(password))).strip()
req.add_header('Proxy-authorization', 'Basic ' + user_pass)
host = unquote(host)
- req.set_proxy(host, type)
+
+ need_proxy = 1
+ if len(self.noproxies) > 0:
+ orig_host = req.get_host()
+ for domain in self.noproxies:
+ if orig_host.count(domain):
+ need_proxy = 0
+ break
+
+ if need_proxy == 1:
+ req.set_proxy(host, type)
+
if orig_type == type:
# let other handlers take care of it
# XXX this only makes sense if the proxy is before the
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment