Skip to content
Myr

New helper method `set_http_proxy` in `Session` to set proxies in a more user friendly way

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

authored by Sylvain MARIE · committed by Sylvain MARIE · parent 9ce669e · Browse files · 2 files+120 added−0 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+120 added−0 removed

  • tests:+35 added−0 removed
  • other:+85 added−0 removed

2 files+120 added−0 removed

modified:requests/sessions.py+85 added
696 unmodified lines
697 r.content698699 return r700701 def set_http_proxy(self,702 http_scheme='http', # type: str703 http_host=None, # type: str704 http_port=80, # type: int705 http_url=None, # type: str706 use_http_proxy_for_https_requests=False, # type: bool707 https_scheme='https', # type: str708 https_host=None, # type: str709 https_port=443, # type: int710 https_url=None, # type: str711 replace=False # type: bool712 ):713 """Update or replace self.proxies with the provided proxy information.714715 This method updates or replaces (depending on the value of `replace`) the dictionary in `self.proxies` with the716 provided information. For each kind of connection (http and https), there are two ways to pass the information:717 either as an url string (`http_url`, `https_url`), or split in schema/host/port, with sensible defaults.718 In addition if the exact same proxy information is to be used for http and https, you can pass only the http719 one and set `use_http_proxy_for_https_requests` to True.720721 See :ref:`Proxies documentation <_proxies>` for details.722723 :param http_host: (optional) a string indicating the http proxy host, for example '10.10.1.10' or 'acme.com'.724 :param http_port: (optional) an int indicating the http proxy port, for example `3128`.725 :param http_scheme: (optional) a string indicating the scheme to use for http proxy. By default this is 'http'726 but you can consider using 'socks5', 'socks5h'. See documentation for details.727 :param http_url: (optional) a string indicating the full http proxy url. For example 'http://10.10.1.10:3128'728 or 'http://user:pass@10.10.1.10:3128/' or 'socks5://user:pass@host:port'.729 Only one of {http_scheme + http_host + http_port} or {http_url} should be provided.730 :param use_http_proxy_for_https_requests: (optional) a boolean indicating whether the information provided for731 the http proxy should be copied for the https proxy. Note that the full url will be copied including the732 scheme (so by default 'http').733 :param https_host: (optional) a string indicating the https proxy host, for example '10.10.1.10' or 'acme.com'.734 :param https_port: (optional) an int indicating the https proxy port, for example `3128`.735 :param https_scheme: (optional) a string indicating the scheme to use for https proxy. By default this is736 'https' but you can consider using 'socks5', 'socks5h'. See documentation for details.737 :param https_url: (optional) a string indicating the full https proxy url. For example 'https://10.10.1.10:3128'738 or 'http://user:pass@10.10.1.10:3128/' or 'socks5://user:pass@host:port'.739 Only one of {https_scheme + https_host + https_port} or {https_url} should be provided.740 :param replace: (optional) a boolean indicating if the provided information should replace the existing one741 (True) or just update it (False, default).742 :return:743 """744 proxies = dict()745746 # HTTPS747 if http_host is not None:748 # (a) scheme + host + port749 if http_url is not None:750 raise ValueError("Only one of `http_host` and `http_url` should be provided")751 proxies['http'] = "%s://%s:%s" % (http_scheme, http_host, http_port)752 elif http_url is not None:753 # (b) full url754 proxies['http'] = http_url755 elif http_port != 80 or http_scheme != 'http':756 raise ValueError("An `http_host` should be provided if you wish to change `http_port` or `http_scheme`")757758 # HTTPS759 if use_http_proxy_for_https_requests:760 # (a) copy the information from http761 if https_host is not None or https_url is not None or https_port != 443 or https_scheme != "https":762 raise ValueError("`use_http_proxy_for_https_requests` was set to `True` but custom information for "763 "https was provided.")764 try:765 proxies['https'] = proxies['http']766 except KeyError:767 raise ValueError("`use_http_proxy_for_https_requests` was set to `True` but no information was "768 "provided for the http proxy")769 elif https_host is not None:770 # (b) scheme + host + port771 if https_url is not None:772 raise ValueError("Only one of `https_host` and `https_url` should be provided")773 proxies['https'] = '%s://%s:%s' % (https_scheme, https_host, https_port)774 elif https_url is not None:775 # (c) full url776 proxies['https'] = https_url777 elif https_port != 443 or https_scheme != 'https':778 raise ValueError("An `https_host` should be provided if you wish to change `https_port` or `https_scheme`")779780 # Replace or update (default) the configuration781 if replace:782 self.proxies = proxies783 else:784 self.proxies.update(proxies)785786 def merge_environment_settings(self, url, proxies, stream, verify, cert):787 """788 Check the environment and merge it with some settings.789
77 unmodified lines
modified:tests/test_requests.py+35 added
149 unmodified lines
150 # Test for issue #3696151 request = requests.Request('GET', ' http://example.com').prepare()152 assert request.url == 'http://example.com/'153154 def test_proxies_shortcut(self):155 """Test that the set_http_proxy helper function works correctly"""156 s = requests.Session()157158 # nominal159 s.set_http_proxy(http_scheme='socks5', http_host='acme.com', http_port=999)160 assert s.proxies == {'http': 'socks5://acme.com:999'}161 s.set_http_proxy(https_scheme='socks5h', https_host='acme.org', https_port=80)162 assert s.proxies == {'http': 'socks5://acme.com:999', 'https': 'socks5h://acme.org:80'}163164 # one can not specify a scheme or a port without passing a host165 with pytest.raises(ValueError):166 s.set_http_proxy(http_scheme='socks5')167 with pytest.raises(ValueError):168 s.set_http_proxy(https_scheme='socks5')169 with pytest.raises(ValueError):170 s.set_http_proxy(http_port=999)171 with pytest.raises(ValueError):172 s.set_http_proxy(https_port=999)173174 # use_http_proxy_for_https_requests requires http related information to be present175 with pytest.raises(ValueError):176 s.set_http_proxy(use_http_proxy_for_https_requests=True)177178 # reuse http info for https179 s.set_http_proxy(http_url='http://10.10.10.10:80', use_http_proxy_for_https_requests=True)180 assert s.proxies == {'http': 'http://10.10.10.10:80', 'https': 'http://10.10.10.10:80'}181182 # replace instead of update183 s.set_http_proxy(https_url='http://10.10.10.20:80', replace=True)184 assert s.proxies == {'https': 'http://10.10.10.20:80'}185186 # https url187 s.set_http_proxy(https_url='http://10.10.10.20:80', replace=True)188189 @pytest.mark.parametrize('scheme', ('http://', 'HTTP://', 'hTTp://', 'HttP://'))190 def test_mixed_case_scheme_acceptable(self, httpbin, scheme):191 s = requests.Session()192 s.proxies = getproxies()
2372 unmodified lines