@@ -6,6 +6,8 @@ import io
66import json
77import re
88import ssl
9+ from urllib.parse import urlparse
10+ from urllib.request import getproxies, proxy_bypass
911
1012import urllib3
1113
@@ -89,14 +91,23 @@ class RESTClientObject:
8991 # https pool manager
9092 self.pool_manager: urllib3.PoolManager
9193
92- if configuration.proxy:
93- if is_socks_proxy_url(configuration.proxy):
94+ parsed = urlparse(configuration.host)
95+ proxy = getproxies().get(parsed.scheme) if configuration.proxy is None else configuration.proxy
96+ if proxy:
97+ if configuration.no_proxy is not None:
98+ if _proxy_bypass(parsed.netloc, configuration.no_proxy):
99+ proxy = None
100+ elif proxy_bypass(parsed.netloc):
101+ proxy = None
102+
103+ if proxy:
104+ if is_socks_proxy_url(proxy):
94105 from urllib3.contrib.socks import SOCKSProxyManager
95- pool_args["proxy_url"] = configuration. proxy
106+ pool_args["proxy_url"] = proxy
96107 pool_args["headers"] = configuration.proxy_headers
97108 self.pool_manager = SOCKSProxyManager(**pool_args)
98109 else:
99- pool_args["proxy_url"] = configuration. proxy
110+ pool_args["proxy_url"] = proxy
100111 pool_args["proxy_headers"] = configuration.proxy_headers
101112 self.pool_manager = urllib3.ProxyManager(**pool_args)
102113 else:
@@ -246,3 +257,27 @@ class RESTClientObject:
246257 raise ApiException(status=0, reason=msg)
247258
248259 return RESTResponse(r)
260+
261+ # Adapted from urllib.request.proxy_bypass_environment
262+ def _proxy_bypass(netloc: str, no_proxy: str):
263+ if no_proxy == '*':
264+ return True
265+
266+ host = netloc.lower()
267+ if match := re.match(r'(.*):([0-9]*)', host):
268+ hostonly, _port = match.groups()
269+ else:
270+ hostonly = host
271+
272+ for name in no_proxy.split(','):
273+ name = name.strip()
274+ if name:
275+ name = name.lstrip('.')
276+ name = name.lower()
277+ if hostonly == name or host == name:
278+ return True
279+ name = '.' + name
280+ if hostonly.endswith(name) or host.endswith(name):
281+ return True
282+
283+ return False
0 commit comments