-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathip_info.py
48 lines (37 loc) · 1.28 KB
/
ip_info.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
import utilities, threading
REQUEST_URL = "http://api.ipinfodb.com/v3/ip-country/?key=a4610f4c5d9c206aa4e94637d50f5db4967d0ba5c482cdb8c75dfbe3583b78c0&format=json"
response = None
class IpGetter(threading.Thread):
def __init__(self):
self.response = None
self.error = None
threading.Thread.__init__(self)
self.setDaemon(True)
def run(self):
try:
self.response = get_response()
except Exception, e:
self.error = e
def get_response(self):
if self.isAlive():
raise RuntimeError("Not finished yet")
elif self.error != None:
raise self.error
else:
return self.response
def get_country_code(self):
return self.get_response()["countryCode"]
def get_country_name(self):
return self.get_response()["countryName"]
def get_response():
global response
if response == None:
response = utilities.load_json(REQUEST_URL)
if response["statusCode"] != "OK":
raise Exception("Code: " + response["statusCode"] +
"Message: " + response["statusMessage"])
return response
def get_country_code():
return get_response()["countryCode"]
def get_country_name():
return get_response()["countryName"]