-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipsearch.py
More file actions
49 lines (43 loc) · 1.39 KB
/
ipsearch.py
File metadata and controls
49 lines (43 loc) · 1.39 KB
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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
import urllib2
import json
from time import sleep
class QueryIpAddressException(Exception) :
def __init__(self,ip) :
Exception.__init__(self,ip)
self.ip = ip
class QueryIpInfo() :
def __init__(self,count = 1024,times = 5) :
self.iptable = dict()
self.count = count
self.times = times
def query(self,ip) :
ip_info = self.iptable.get(ip)
if ip_info != None :
return ip_info
else :
if len(self.iptable) > self.count :
self.iptable.clear()
return self.__query__(ip)
def __query__(self,ip) :
url = 'http://ip.taobao.com/service/getIpInfo.php?ip=%s'%ip
times = self.times
while times > 0 :
try :
times = times - 1
link = urllib2.urlopen(url)
data = json.loads(link.read())
link.close()
if data['code'] != 0 :
raise QueryIpAddressException(ip)
self.iptable[ip] = data['data']
return (data['data'])
except (Exception) as e :
if times > 0:
sleep(1)
pass
else :
raise QueryIpAddressException(e,ip)
queryip = QueryIpInfo()