-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoip_client.r2py
143 lines (107 loc) · 3.4 KB
/
geoip_client.r2py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
<Author>
Evan Meagher
<Start Date>
Nov 26, 2009
<Description>
XMl-RPC client for remote GeoIP server. Given an IP:port of a GeoIP
XML-RPC server, allows location lookup of hostnames and IP addresses.
<Usage>
client = geoip_client(server_address)
Where server_address is the ip address of a remote GeoIP XMl-RPC server.
"""
from repyportability import *
add_dy_support(locals())
xmlrpc_client = dy_import_module('xmlrpc_client.r2py')
checkipaddr = dy_import_module('check_ip_address.r2py')
"""
Initialize global GeoIP XML-RPC client object to None
Note: client is stored in wrapper list to avoid using mycontext dict
"""
geoip_clientlist = [None]
def geoip_init_client(url="http://geoipserver.poly.edu:12679"):
"""
<Purpose>
Create a new GeoIP XML-RPC client object.
<Arguments>
url:
URL (protocol://ip:port) of GeoIP XML-RPC server.
<Exceptions>
None.
<Side Effects>
Inserts GeoIP XML-RPC client as first element of global
geoip_clientlist.
<Returns>
None.
"""
# Store XML-RPC client globally in list to avoid using mycontext dict
geoip_clientlist[0] = xmlrpc_client.xmlrpc_client_Client(url)
def geoip_record_by_addr(addr, timeout=5):
"""
<Purpose>
Request location data of provided IP address from GeoIP XML-RPC server
<Arguments>
addr:
IP address of which to look up location
timeout
How long we should wait for a response.
<Exceptions>
None.
<Side Effects>
None.
<Returns>
Dictionary of location data of provided IP address.
"""
validip = checkipaddr.is_valid_ip_address(addr)
if(validip == True):
return geoip_clientlist[0].send_request("record_by_addr", (addr,), timeout)
else:
sys.exit("Please re-enter valid ip address")
def geoip_record_by_name(name, timeout=5):
"""
<Purpose>
Request location data of provided hostname from GeoIP XML-RPC server
<Arguments>
name:
Hostname of which to look up location
timeout
How long we should wait for a response.
<Exceptions>
None.
<Side Effects>
None.
<Returns>
Dictionary of location data of provided hostname.
"""
return geoip_clientlist[0].send_request("record_by_name", (name,), timeout)
def geoip_location_str(location_dict):
"""
<Purpose>
Pretty-prints a location specified by location_dict as a comma-separated
list. Prints location info as specifically as it can, according to the
format 'CITY, STATE/PROVINCE, COUNTRY'.
location_dict['city'], location_dict['region_name'], and
location_dict['country_name'] are added if defined, and
location_dict['region_name'] is added if the location is in the US or
Canada.
<Arguments>
location_dict
Dictionary of location information, as returned by a call to
geoip_record_by_addr or geoip_record_by_name.
<Exceptions>
None.
<Side Effects>
None.
<Returns>
A string representation of a location.
"""
location_str = ""
if 'city' in location_dict:
location_str = location_str + location_dict['city'] + ", "
if 'country_name' in location_dict:
# If location is in the US or Canada, include the state/province
if location_dict['country_name'] in ['United States', 'Canada'] \
and 'region_name' in location_dict:
location_str = location_str + location_dict['region_name'] + ", "
location_str = location_str + location_dict['country_name']
return location_str