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 = 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()
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。