-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.py
More file actions
45 lines (31 loc) · 1.58 KB
/
Copy pathRequest.py
File metadata and controls
45 lines (31 loc) · 1.58 KB
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
import logging
import time
import urllib3
from Checks import Checks
from HttpStatuses import LogStatus
def __request(uri, page_description, method, site, config, fields=(), second_chance=False):
def too_slow():
return not Checks.time_acceptable(time_elapsed, config.get("acceptable_time", 100))
def should_give_second_chance():
return too_slow() and config.get("give_second_chance", True) and (not second_chance)
def get_uri():
return uri.replace(" ", "%20")
def get_headers():
headers = urllib3.make_headers(keep_alive=True, accept_encoding=True)
headers["cookie"] = site.get("cookies", "")
return headers
logging.info("Getting %s (%s)" % (page_description, get_uri()))
http = urllib3.PoolManager(retries=False)
request_start_time = time.time()
get_uri()
http_request = http.request(method, get_uri(), fields=fields, headers=get_headers())
time_elapsed = time.time() - request_start_time
if should_give_second_chance():
logging.info("Doing second chance request for %s (too slow first time at %d seconds)" % (uri, time_elapsed))
return __request(uri, page_description, method, site, config, fields, second_chance=True)
LogStatus.log_status(http_request, uri, page_description, time_elapsed, site, config)
return http_request
def get_request(uri, site, config, page_description=""):
return __request(uri, page_description, "GET", site, config)
def post_request(uri, site, config, fields, page_description):
return __request(uri, page_description, "POST", site, config, fields)