Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Authors: Sylvain MARIE <sylvain.marie@se.com> 

2# + All contributors to <https://github.com/smarie/python-azureml-client> 

3# 

4# License: 3-clause BSD, <https://github.com/smarie/python-azureml-client/blob/master/LICENSE> 

5try: # python 3+ 

6 from urllib.parse import urlparse 

7except ImportError: 

8 from urlparse import urlparse 

9 

10from requests import Session 

11from valid8 import validate 

12 

13 

14def parse_proxy_info(proxy_url 

15 ): 

16 """ 

17 Parses a proxy url 

18 

19 :param proxy_url: 

20 :return: 

21 """ 

22 o = urlparse(proxy_url) 

23 

24 validate('hostname', o.hostname, min_len=1) 

25 validate('port', o.port) 

26 validate('scheme', o.scheme, is_in={'http', 'https'}, 

27 help_msg="Only http and https protocols are supported for http(s) proxies. " 

28 "Found: '{var_value}' from '%s'" % proxy_url) 

29 

30 return o.hostname, o.port, o.scheme 

31 

32 

33def set_http_proxy(session, # type: Session 

34 http_scheme='http', # type: str 

35 http_host=None, # type: str 

36 http_port=80, # type: int 

37 http_url=None, # type: str 

38 use_http_proxy_for_https_requests=False, # type: bool 

39 https_scheme='https', # type: str 

40 https_host=None, # type: str 

41 https_port=443, # type: int 

42 https_url=None, # type: str 

43 replace=False # type: bool 

44 ): 

45 """Update or replace session.proxies with the provided proxy information. 

46 

47 This method updates or replaces (depending on the value of `replace`) the dictionary in `session.proxies` with the 

48 provided information. For each kind of connection (http and https), there are two ways to pass the information: 

49 either as an url string (`http_url`, `https_url`), or split in schema/host/port, with sensible defaults. 

50 In addition if the exact same proxy information is to be used for http and https, you can pass only the http 

51 one and set `use_http_proxy_for_https_requests` to True. 

52 

53 See the requests proxies documentation for details: https://requests.readthedocs.io/en/master/user/advanced/#proxies 

54 

55 Note: this was proposed as a PR in requests: https://github.com/psf/requests/pull/5670 

56 but because of the feature freeze it was then transformed into a simple doc update. 

57 

58 :param http_host: (optional) a string indicating the http proxy host, for example '10.10.1.10' or 'acme.com'. 

59 :param http_port: (optional) an int indicating the http proxy port, for example `3128`. 

60 :param http_scheme: (optional) a string indicating the scheme to use for http proxy. By default this is 'http' 

61 but you can consider using 'socks5', 'socks5h'. See documentation for details. 

62 :param http_url: (optional) a string indicating the full http proxy url. For example 'http://10.10.1.10:3128' 

63 or 'http://user:pass@10.10.1.10:3128/' or 'socks5://user:pass@host:port'. 

64 Only one of {http_scheme + http_host + http_port} or {http_url} should be provided. 

65 :param use_http_proxy_for_https_requests: (optional) a boolean indicating whether the information provided for 

66 the http proxy should be copied for the https proxy. Note that the full url will be copied including the 

67 scheme (so by default 'http'). 

68 :param https_host: (optional) a string indicating the https proxy host, for example '10.10.1.10' or 'acme.com'. 

69 :param https_port: (optional) an int indicating the https proxy port, for example `3128`. 

70 :param https_scheme: (optional) a string indicating the scheme to use for https proxy. By default this is 

71 'https' but you can consider using 'socks5', 'socks5h'. See documentation for details. 

72 :param https_url: (optional) a string indicating the full https proxy url. For example 'https://10.10.1.10:3128' 

73 or 'http://user:pass@10.10.1.10:3128/' or 'socks5://user:pass@host:port'. 

74 Only one of {https_scheme + https_host + https_port} or {https_url} should be provided. 

75 :param replace: (optional) a boolean indicating if the provided information should replace the existing one 

76 (True) or just update it (False, default). 

77 :return: 

78 """ 

79 proxies = dict() 

80 

81 # HTTPS 

82 if http_host is not None: 82 ↛ 84line 82 didn't jump to line 84, because the condition on line 82 was never true

83 # (a) scheme + host + port 

84 if http_url is not None: 

85 raise ValueError("Only one of `http_host` and `http_url` should be provided") 

86 proxies['http'] = "%s://%s:%s" % (http_scheme, http_host, http_port) 

87 elif http_url is not None: 87 ↛ 89line 87 didn't jump to line 89, because the condition on line 87 was never true

88 # (b) full url 

89 parse_proxy_info(http_url) 

90 proxies['http'] = http_url 

91 elif http_port != 80 or http_scheme != 'http': 91 ↛ 92line 91 didn't jump to line 92, because the condition on line 91 was never true

92 raise ValueError("An `http_host` should be provided if you wish to change `http_port` or `http_scheme`") 

93 

94 # HTTPS 

95 if use_http_proxy_for_https_requests: 95 ↛ 97line 95 didn't jump to line 97, because the condition on line 95 was never true

96 # (a) copy the information from http 

97 if https_host is not None or https_url is not None or https_port != 443 or https_scheme != "https": 

98 raise ValueError("`use_http_proxy_for_https_requests` was set to `True` but custom information for " 

99 "https was provided.") 

100 try: 

101 proxies['https'] = proxies['http'] 

102 except KeyError: 

103 raise ValueError("`use_http_proxy_for_https_requests` was set to `True` but no information was " 

104 "provided for the http proxy") 

105 elif https_host is not None: 105 ↛ 107line 105 didn't jump to line 107, because the condition on line 105 was never true

106 # (b) scheme + host + port 

107 if https_url is not None: 

108 raise ValueError("Only one of `https_host` and `https_url` should be provided") 

109 proxies['https'] = '%s://%s:%s' % (https_scheme, https_host, https_port) 

110 elif https_url is not None: 110 ↛ 112line 110 didn't jump to line 112, because the condition on line 110 was never true

111 # (c) full url 

112 proxies['https'] = https_url 

113 elif https_port != 443 or https_scheme != 'https': 113 ↛ 114line 113 didn't jump to line 114, because the condition on line 113 was never true

114 raise ValueError("An `https_host` should be provided if you wish to change `https_port` or `https_scheme`") 

115 

116 # Replace or update (default) the configuration 

117 if len(proxies) > 0: 117 ↛ 118line 117 didn't jump to line 118, because the condition on line 117 was never true

118 if replace: 

119 session.proxies = proxies 

120 else: 

121 session.proxies.update(proxies) 

122 

123 # IMPORTANT : otherwise the environment variables will always have precedence over user-provided settings 

124 session.trust_env = False