首页 > 代码库 > Python统计nginx脚本信息

Python统计nginx脚本信息

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 import urllib2
 4 import json
 5 import subprocess
 6 import threading
 7 
 8 #统计10个最长访问的ip
 9 ip_raw = subprocess.Popen("cut -d ‘ ‘ -f1 host.access.log.* | sort | uniq -c | sort -rn | head -n 10 | awk ‘{print $2}‘",shell=True,stdout=subprocess.PIPE)
10 iplist = ip_raw.stdout.readlines()
11 
12 #淘宝ip库接口
13 url = "http://ip.taobao.com/service/getIpInfo.php?ip="
14 
15 class regionip(threading.Thread):
16     def __init__(self):
17         threading.Thread.__init__(self,ip)
18         self.ip = ip
19     def run(self):
20         try:
21             data = http://www.mamicode.com/urllib2.urlopen(url + ip,timeout=1).read()
22             datadict=json.loads(data)
23             if  datadict["code"] == 0:
24                 print "%s %s %s %s %s " % (datadict[data][ip],datadict["data"]["country"],datadict["data"]["region"],datadict["data"]["city"],datadict["data"]["isp"])
25         except Exception,e:
26             print "%s\t%s" % (ip,e)
27 
28 if __name__=="__main__":
29     for ip in iplist:
30         t = regionip(ip)
31         t.start()
32         t.join()