-
Notifications
You must be signed in to change notification settings - Fork 0
/
HostInformation.py
81 lines (70 loc) · 2.77 KB
/
HostInformation.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import sys
import requests
from requests.models import MissingSchema, ReadTimeoutError
from requests.packages.urllib3.exceptions import ConnectTimeoutError, MaxRetryError
from requests.packages.urllib3.connectionpool import HTTPConnectionPool
from urllib3.exceptions import InsecureRequestWarning
import json
def _make_request(self, conn, method, url, **kwargs):
response = self._old_make_request(conn, method, url, **kwargs)
sock = getattr(conn, 'sock', False)
if sock:
setattr(response, 'peer', sock.getpeername())
else:
setattr(response, 'peer', None)
return response
HTTPConnectionPool._old_make_request = HTTPConnectionPool._make_request
HTTPConnectionPool._make_request = _make_request
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
class HostInformation():
def __init__(self, host, timeout):
self.Host = host
self.Timeout = timeout
self.IPAddress = ""
self.Port = 0
self.Country = ""
self.City = ""
self.Timezone = ""
self.ISP = ""
self.Organization = ""
self.Latitude = ""
self.Longitude = ""
def MakeRequest(self, host):
try:
response = requests.get(host, timeout=self.Timeout, verify=False)
except requests.exceptions.ReadTimeout:
print("[ERROR] ReadTimeout")
return None
except requests.exceptions.ConnectTimeout:
print("[ERROR] ConnectTimeout")
return None
except MaxRetryError:
print("[ERROR] MaxRetryError")
return None
except MissingSchema:
print("[ERROR] MissingSchema")
return None
except requests.exceptions.ConnectionError:
print("[ERROR] ConnectionError")
return None
return response
def SetHostInformation(self):
if self.Host != None:
response = self.MakeRequest(self.Host)
if response == None:
return -1
else:
self.IPAddress = response.raw._original_response.peer[0]
self.Port = response.raw._original_response.peer[1]
geoInfoResponse = self.MakeRequest("http://ip-api.com/json/" + self.IPAddress) # has no relation with owner of this code. Please implement this line as your own responsibility
if geoInfoResponse == None:
return -1
jsonResponse = json.loads(geoInfoResponse.text)
self.Country = jsonResponse["country"]
self.City = jsonResponse["regionName"]
self.Timezone = jsonResponse["timezone"]
self.Latitude = jsonResponse["lat"]
self.Longitude = jsonResponse["lon"]
self.ISP = jsonResponse["isp"]
self.Organization = jsonResponse["org"]
return 1