forked from snowflakedb/snowflake-connector-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.py
66 lines (57 loc) · 1.96 KB
/
proxy.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2018 Snowflake Computing Inc. All right reserved.
#
import botocore.endpoint
from .compat import (TO_UNICODE)
"""
Proxy, shared across all connections
"""
PROXY_HOST = None
PROXY_PORT = None
PROXY_USER = None
PROXY_PASSWORD = None
def set_proxies(proxy_host, proxy_port, proxy_user=None, proxy_password=None):
"""
Set proxy dict for requests
"""
PREFIX_HTTP = 'http://'
PREFIX_HTTPS = 'https://'
proxies = None
if proxy_host and proxy_port:
if proxy_host.startswith(PREFIX_HTTP):
proxy_host = proxy_host[len(PREFIX_HTTP):]
elif proxy_host.startswith(PREFIX_HTTPS):
proxy_host = proxy_host[len(PREFIX_HTTPS):]
if proxy_user or proxy_password:
proxy_auth = u'{proxy_user}:{proxy_password}@'.format(
proxy_user=proxy_user if proxy_user is not None else '',
proxy_password=proxy_password if proxy_password is not
None else ''
)
else:
proxy_auth = u''
proxies = {
u'http': u'http://{proxy_auth}{proxy_host}:{proxy_port}'.format(
proxy_host=proxy_host,
proxy_port=TO_UNICODE(proxy_port),
proxy_auth=proxy_auth,
),
u'https': u'http://{proxy_auth}{proxy_host}:{proxy_port}'.format(
proxy_host=proxy_host,
proxy_port=TO_UNICODE(proxy_port),
proxy_auth=proxy_auth,
),
}
return proxies
def _get_proxies(self, url):
return set_proxies(
PROXY_HOST,
PROXY_PORT,
PROXY_USER,
PROXY_PASSWORD) or original_get_proxies(self, url)
# Monkey patch for all connections for AWS API. This is mainly for PUT
# and GET commands
original_get_proxies = botocore.endpoint.EndpointCreator._get_proxies
botocore.endpoint.EndpointCreator._get_proxies = _get_proxies