-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrequest_mrclw.py
40 lines (31 loc) · 1.51 KB
/
request_mrclw.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
import time
from socket import timeout
import requests
import urllib
from requests.exceptions import ConnectionError, HTTPError
class RequestMrclw:
def __init__(self):
self.protocol = 'https'
self.timeout = 8
self.header = {}
def send_request(self, _target: str, _value_header: str):
if _target:
target_url = None
target_url = self.protocol + '://'+_target
try:
start = time.time()
obj_urllib = urllib.request.Request(target_url)
obj_urllib.add_header("Content-type", "application/x-www-form-urlencoded")
obj_urllib.add_header("User-Agent", 'Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0')
obj_request = urllib.request.urlopen(obj_urllib,timeout=self.timeout)
obj_request_result = str(obj_request.read().decode('utf-8'))
time_final = (f'in {time.time() - start:.2f}s')
if obj_request_result:
return obj_request.url, obj_request_result, obj_request.status, time_final
return target_url, 'Empry', obj_request.status, time_final
except urllib.error.HTTPError as ehttp:
return target_url, 'HTTP Error!', ehttp.code, str()
except timeout:
return target_url, 'Time!', 'Socket Timed Out', str()
except urllib.error.URLError as eurl:
return target_url, 'URL Error!', eurl.reason, str()