-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproxies.py
43 lines (34 loc) · 1.34 KB
/
proxies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""TcEx Framework Module"""
# standard library
from urllib.parse import quote
from ..input.field_type.sensitive import Sensitive # type: ignore # pylint: disable=import-error
def proxies(
proxy_host: str | None,
proxy_port: int | None,
proxy_user: str | None,
proxy_pass: Sensitive | str | None,
) -> dict:
"""Format the proxy configuration for Python Requests module.
Generates a dictionary for use with the Python Requests module format
when proxy is required for remote connections.
**Example Response**
::
{
"http": "http://user:[email protected]:3128/",
"https": "http://user:[email protected]:3128/"
}
"""
_proxies = {}
if proxy_host is not None and proxy_port is not None:
proxy_auth = ''
if proxy_user is not None and proxy_pass is not None:
proxy_user = quote(proxy_user, safe='~')
if isinstance(proxy_pass, Sensitive):
proxy_pass_ = quote(proxy_pass.value, safe='~') # type: ignore
else:
proxy_pass_ = quote(proxy_pass, safe='~')
# proxy url with auth
proxy_auth = f'{proxy_user}:{proxy_pass_}@'
proxy_url = f'{proxy_auth}{proxy_host}:{proxy_port}'
_proxies = {'http': f'http://{proxy_url}', 'https': f'http://{proxy_url}'}
return _proxies