-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipinfodb.py
110 lines (90 loc) · 3.39 KB
/
ipinfodb.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
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
# *-* coding: utf-8*-*
'''
ipinfodb.py
Copyright 2010 Martin Alderete
ipinfodb is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
ipinfodb is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pyPlugin; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
<<END_LICENSE>>
'''
import socket
import urllib2
import urllib
import simplejson
class API(object):
'''
Usage
For city precision, simply do a query at this address (if you omit the IP
parameter it will check with your own IP) :
http://api.ipinfodb.com/v3/ip-city/?key=<your_api_key>&ip=74.125.45.100&format=json
{'countryCode': 'AR', 'cityName': 'BUENOS AIRES', 'zipCode': '-',
'longitude': '-58.47', 'countryName': 'ARGENTINA', 'latitude':
'-34.613', 'timeZone': '-03:00', 'statusCode': 'OK', 'ipAddress':
'190.191.58.56', 'statusMessage': '', 'regionName': 'DISTRITO FEDERAL'}
For country precision (faster), simply do a query with the following API :
http://api.ipinfodb.com/v3/ip-country/?key=<your_api_key>&ip=74.125.45.100&format=json
{'countryName': 'ARGENTINA', 'ipAddress': '190.191.58.56',
'countryCode': 'AR', 'statusMessage': '', 'statusCode': 'OK'}
@author: Martin Alderete ([email protected])
'''
def __init__(self, api_key):
self._api_key = api_key
def getCityUrl(self):
return 'http://api.ipinfodb.com/v3/ip-city/'
def getCountryUrl(self):
return 'http://api.ipinfodb.com/v3/ip-country/'
def GetCity(self, ip):
query_parameters = {
'key': self._api_key,
'ip': ip,
'format': 'json'
}
url = self.getCityUrl() + '?' + urllib.urlencode(query_parameters)
try:
file_obj = urllib2.urlopen(url)
except urllib2.URLError, e:
print "URLError"
raise
except urllib2.HTTPError, e:
print "HTTPError"
raise
data = file_obj.read()
data_dict = self._parseResponse(data)
file_obj.close()
return data_dict
def GetCountry(self, ip):
query_parameters = {
'key': self._api_key,
'ip': ip,
'format': 'json'
}
url = self.getCountryUrl() + '?' + urllib.urlencode(query_parameters)
try:
file_obj = urllib2.urlopen(url)
except urllib2.URLError, e:
print "URLError"
raise
except urllib2.HTTPError, e:
print "HTTPError"
raise
data = file_obj.read()
data_dict = self._parseResponse(data)
file_obj.close()
return data_dict
def _parseResponse(self, data):
return simplejson.loads(data)
def get_ip_from_name(name, port):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((name, port))
return s.getpeername()[0]
except Exception, e:
print "Socket Error: %s" % e
raise