Represents a proxy configuration dictionary and additional settings. This class represents a proxy configuration dictionary and provides utility functions to retreive well structured proxy urls and proxy headers from the proxy configuration dictionary.
| 218 | |
| 219 | |
| 220 | class ProxyConfiguration: |
| 221 | """Represents a proxy configuration dictionary and additional settings. |
| 222 | |
| 223 | This class represents a proxy configuration dictionary and provides utility |
| 224 | functions to retreive well structured proxy urls and proxy headers from the |
| 225 | proxy configuration dictionary. |
| 226 | """ |
| 227 | |
| 228 | def __init__(self, proxies=None, proxies_settings=None): |
| 229 | if proxies is None: |
| 230 | proxies = {} |
| 231 | if proxies_settings is None: |
| 232 | proxies_settings = {} |
| 233 | |
| 234 | self._proxies = proxies |
| 235 | self._proxies_settings = proxies_settings |
| 236 | |
| 237 | def proxy_url_for(self, url): |
| 238 | """Retrieves the corresponding proxy url for a given url.""" |
| 239 | parsed_url = urlparse(url) |
| 240 | proxy = self._proxies.get(parsed_url.scheme) |
| 241 | if proxy: |
| 242 | proxy = self._fix_proxy_url(proxy) |
| 243 | return proxy |
| 244 | |
| 245 | def proxy_headers_for(self, proxy_url): |
| 246 | """Retrieves the corresponding proxy headers for a given proxy url.""" |
| 247 | headers = {} |
| 248 | username, password = self._get_auth_from_url(proxy_url) |
| 249 | if username and password: |
| 250 | basic_auth = self._construct_basic_auth(username, password) |
| 251 | headers['Proxy-Authorization'] = basic_auth |
| 252 | return headers |
| 253 | |
| 254 | @property |
| 255 | def settings(self): |
| 256 | return self._proxies_settings |
| 257 | |
| 258 | def _fix_proxy_url(self, proxy_url): |
| 259 | if proxy_url.startswith('http:') or proxy_url.startswith('https:'): |
| 260 | return proxy_url |
| 261 | elif proxy_url.startswith('//'): |
| 262 | return 'http:' + proxy_url |
| 263 | else: |
| 264 | return 'http://' + proxy_url |
| 265 | |
| 266 | def _construct_basic_auth(self, username, password): |
| 267 | auth_str = f'{username}:{password}' |
| 268 | encoded_str = b64encode(auth_str.encode('ascii')).strip().decode() |
| 269 | return f'Basic {encoded_str}' |
| 270 | |
| 271 | def _get_auth_from_url(self, url): |
| 272 | parsed_url = urlparse(url) |
| 273 | try: |
| 274 | return unquote(parsed_url.username), unquote(parsed_url.password) |
| 275 | except (AttributeError, TypeError): |
| 276 | return None, None |
| 277 |
no outgoing calls