-
Notifications
You must be signed in to change notification settings - Fork 6
/
request_utils.py
51 lines (42 loc) · 1.52 KB
/
request_utils.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
from requests import Request, Session
from requests.exceptions import Timeout, SSLError
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager
import json
clients = {}
class SSLAdapter(HTTPAdapter):
def __init__(self, ssl_version=None, **kwargs):
self.ssl_version = ssl_version
super(SSLAdapter, self).__init__(**kwargs)
def init_poolmanager(self, connections, maxsize, block=False):
self.poolmanager = PoolManager(num_pools=connections,
maxsize=maxsize, block=block,
ssl_version=self.ssl_version)
def send_request(method, url, payload=None, headers=None, client=None):
if ( client == None ):
s = Session()
s.mount('https://', SSLAdapter('TLSv1'))
elif ( clients.get(client, False) ):
s = clients[client]
else:
s = Session()
s.mount('https://', SSLAdapter('TLSv1'))
clients[client] = s
if ( payload == None ):
request = Request(method, url, headers=headers)
r = s.prepare_request(request)
else:
if ( headers == None ):
headers = {}
request = Request(method, url,
data=json.dumps(payload), headers=headers)
r = s.prepare_request(request)
try:
response = s.send(r, verify=False, timeout=1)
except Timeout, SSLError:
return send_request(method, url, payload, client)
return {
'success': response.ok,
'code': response.status_code,
'content': response.content
}