Skip to content
Myr

Added possibility to use IP ranges (ex. 192.168.1.0/24) to no_proxy environment variable

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kamil Madac · committed by Kamil Madac · parent 8477355 · Browse files · 2 files+31 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+31 added−5 removed

  • other:+31 added−5 removed

2 files+31 added−5 removed

modified:requests/utils.py+22 added-5 removed
15 unmodified lines
16import os17import platform18import re19import sys20import netaddr2122from . import __version__23from . import certs24from .compat import parse_http_list as _parse_list_header
395 unmodified lines
420 # We need to check whether we match here. We need to see if we match421 # the end of the netloc, both with and without the port.422 no_proxy = no_proxy.replace(' ', '').split(',')423423 for host in no_proxy:424 if netloc.endswith(host) or netloc.split(':')[0].endswith(host):425 # The URL does match something in no_proxy, so we don't want426 # to apply the proxies on this URL.427 return {}424 ip = None425 try:426 ip = netaddr.IPAddress(netloc.split(':')[0])427 except netaddr.AddrFormatError:428 pass429430 if ip:431 for proxy_ip in no_proxy:432 proxy_ipaddress = None433 try:434 proxy_ipaddress = netaddr.IPNetwork(proxy_ip)435 except (netaddr.AddrFormatError, ValueError):436 continue437 if proxy_ipaddress and ip in proxy_ipaddress:438 return {}439 else:440 for host in no_proxy:441 if netloc.endswith(host) or netloc.split(':')[0].endswith(host):442 # The URL does match something in no_proxy, so we don't want443 # to apply the proxies on this URL.444 return {}445446 # If the system proxy settings indicate that this URL should be bypassed,447 # don't proxy.448 if proxy_bypass(netloc):449 return {}
151 unmodified lines
modified:test_requests.py+9 added
908 unmodified lines
909 pass910 else:911 assert super_len(cStringIO.StringIO('but some how, some way...')) == 25912913 def test_get_environ_proxies_ip_ranges(self):914 """ Ensures that IP addresses are correctly matches with ranges in no_proxy variable """915 from requests.utils import get_environ_proxies916 os.environ['no_proxy'] = "127.0.0.1,localhost.localdomain,192.168.0.0/24,172.16.1.1"917 assert get_environ_proxies('http://192.168.0.1:5000/') == {}918 assert get_environ_proxies('http://192.168.0.1/') == {}919 assert get_environ_proxies('http://172.16.1.1/') == {}920 assert get_environ_proxies('http://192.168.1.1:5000/') == {'no': os.environ['no_proxy']}921922if __name__ == '__main__':923 unittest.main()