Last active
April 15, 2020 13:01
-
-
Save nyov/b64dd1b5fe22dfd9784e to your computer and use it in GitHub Desktop.
Test environment proxy settings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import print_function | |
import sys, os | |
# This test should show how urllib.proxy_bypass_environment() | |
# doesn't handle proxy environment variables correctly | |
# on *NIX systems. | |
# (It does not understand IPs or CIDR notations.) | |
# | |
# For a possible solution see the `requests` library: | |
# https://github.com/kennethreitz/requests/blob/b137472936cbe6a6acabab538c1d05ed4c7da638/requests/utils.py#L498-L514 | |
# for unix environment | |
if sys.platform == 'darwin' or os.name == 'nt': | |
print( | |
"\nThis test may produce correct results on YOUR platform." | |
" (I hope so.)\n" | |
) | |
try: | |
from urllib import proxy_bypass | |
from urlparse import urlparse | |
except ImportError: | |
from urllib.request import proxy_bypass | |
from urllib.parse import urlparse | |
from requests.utils import should_bypass_proxies | |
os.environ['http_proxy'] = 'http://10.1.1.1:3128' | |
os.environ['https_proxy'] = '' | |
os.environ['no_proxy'] = 'localhost,127.0.0.1/8,10.1.0.0/16,.local' | |
get_env = lambda k: os.environ.get(k.lower()) or os.environ.get(k.upper()) or '' | |
test_urls = [ | |
'http://www.python.org:80', | |
'https://www.python.org:443', | |
'http://localhost/', | |
'http://mydomain.local/', | |
'https://10.1.1.1:443/', | |
'http://10.2.2.2/', | |
'http://127.0.0.1/', | |
] | |
# test | |
if __name__ == "__main__": | |
v = '%s.%s.%s' % (sys.version_info[0], sys.version_info[1], sys.version_info[2]) | |
print('Python version %s' % v) | |
print() | |
print('HTTP_PROXY is: ' + get_env('HTTP_PROXY')) | |
print('HTTPS_PROXY is: ' + get_env('HTTPS_PROXY')) | |
print('NO_PROXY is: ' + get_env('NO_PROXY')) | |
print() | |
print('- urllib test -') | |
for url in test_urls: | |
netloc = urlparse(url).netloc | |
proxied = 'NOT ' if proxy_bypass(netloc) else '' | |
result = "URL %s request will %sbe proxied." % (url, proxied) | |
print(result) | |
print() | |
print('- requests test -') | |
for url in test_urls: | |
proxied = 'NOT ' if should_bypass_proxies(url) else '' | |
result = "URL %s request will %sbe proxied." % (url, proxied) | |
print(result) | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment