python分析nginx日志的ip(來源)-創(chuàng)新互聯(lián)

#!/usr/bin/env python #_*_coding:utf-8 _*_ __author__ = 'gaogd' import datetime,threading import sys, os, urllib2, json reload(sys) sys.setdefaultencoding('utf8') ips = {}  # ip作為字典的key,訪問次數(shù)做value iplist = []  # 遍歷日志中的ip,相同的ip也會(huì)記錄到列表,插入數(shù)據(jù)庫 fh = open("./ip.txt", "r").readlines()  # 我的是把日志和代碼在一個(gè)目錄下面 for line in fh:     ip = line.split(" ")[0]     if 6 < len(ip) <= 15:         ips[ip] = ips.get(ip, 0) + 1         alist = iplist.append(ip) def get_ip_area(ip,num):     try:         apiurl = "http://ip.taobao.com/service/getIpInfo.php?ip=%s" %ip         content = urllib2.urlopen(apiurl).read()         data = json.loads(content)['data']         code = json.loads(content)['code']         if code == 0: # success             country=(data['country'])             area = (data['area'])             region = (data['region'])             city = (data['city'])             ip = (data['ip'])             print(data['country']),(data['area']),(data['region']),(data['city']),(data['ip'])             line = data['country'],data['area'],data['region'],data['city'],data['ip']             data = "%s,%s,%s,%s,%s\n" % line             file='./checkiparea%s.txt' %num             with open(file, 'a+') as f:                  f.write(data)         else:             print data             # data = "%s,\n" % data             # with open('./checkiparea.txt', 'a+') as f:             #     f.write(data)     except Exception as ex:         print ex def getip(num,num2):     for ip in iplist[num:num2]:         get_ip_area(ip,num) if __name__ == '__main__':     for i in range(300):         number=i*10000         t = threading.Thread(target=getip, args=(number,number*(1+i)))         t.start()

Python 腳本如下:

成都創(chuàng)新互聯(lián)公司始終堅(jiān)持【策劃先行,效果至上】的經(jīng)營(yíng)理念,通過多達(dá)十余年累計(jì)超上千家客戶的網(wǎng)站建設(shè)總結(jié)了一套系統(tǒng)有效的營(yíng)銷解決方案,現(xiàn)已廣泛運(yùn)用于各行各業(yè)的客戶,其中包括:柔性防護(hù)網(wǎng)等企業(yè),備受客戶稱揚(yáng)。#!/usr/bin/env python #_*_coding:utf-8 _*_ __author__ = 'gaogd' import MySQLdb as mysql import datetime import sys, os, urllib2, json db = mysql.connect(user="root",passwd="xxxx@2015",db="intest",host="192.168.10.12") #數(shù)據(jù)庫連接信息 db.autocommit(True) cur = db.cursor() cur.execute('set names utf8') addtime = datetime.datetime.now() ips = {}        #ip作為字典的key,訪問次數(shù)做value iplist = []     #遍歷日志中的ip,相同的ip也會(huì)記錄到列表,插入數(shù)據(jù)庫 fh = open("./access_test.log", "r").readlines()                                                               #我的是把日志和代碼在一個(gè)目錄下面 for line in fh:     ip = line.split(" ")[0]     print ip     if 6 < len(ip) <=15:         ips[ip] = ips.get(ip, 0) + 1                  alist = iplist.append(ip) for key,value in ips.items():     listinfo = str(ips)     sql = 'insert into ipinfo(ipaddress,countip) value ("%s","%s")' % (key,value)     cur.execute(sql) def get_ip_area(ip):     try:         apiurl = "http://ip.taobao.com/service/getIpInfo.php?ip=%s" %ip         content = urllib2.urlopen(apiurl).read()         data = json.loads(content)['data']         code = json.loads(content)['code']         if code == 0: # success             country=(data['country'])             area = (data['area'])             region = (data['region'])             city = (data['city'])             ip = (data['ip'])             print(data['country']),(data['area']),(data['region']),(data['city']),(data['ip'])             sql = 'insert into whereip (country,area,region,city,ip,time) value ("%s","%s","%s","%s","%s","%s")' % (country.encode("utf-8"),area.encode("utf-8"),region.encode("utf-8"),city.encode("utf-8"),ip.encode("utf-8"),addtime)             cur.execute(sql)             print 'sql:',sql         else:             print data     except Exception as ex:         print ex if __name__ == '__main__':     for ip in iplist:         get_ip_area(ip)                   ################################################ '''    radiansdict.get(key, default=None) 返回指定鍵的值,如果值不在字典中返回default值 '''

測(cè)試淘寶的ip庫,拿到的數(shù)據(jù)內(nèi)容如下:

http://ip.taobao.com/service/getIpInfo.php?ip=66.249.65.183

python分析nginx日志的ip(來源)

獲取到的字典在http://tool.oschina.net/codeformat/json 進(jìn)行格式化得到下面格式
{     "code": 0,      "data": {         "country": "中國",          "country_id": "CN",          "area": "華南",          "area_id": "800000",          "region": "廣東省",          "region_id": "440000",          "city": "深圳市",          "city_id": "440300",          "county": "",          "county_id": "-1",          "isp": "電信",          "isp_id": "100017",          "ip": "113.97.193.87"     }}

數(shù)據(jù)數(shù)據(jù)庫語句

mysql> create database intest ; Query OK, 1 row affected (0.00 sec) mysql> use intest; Database changed mysql> create table ipinfo(id int auto_increment primary key,ipaddress varchar(200),countip int); Query OK, 0 rows affected (0.22 sec) mysql> create table whereip(id int primary key auto_increment,country varchar(100),area varchar(100),region varchar(100),city varchar(100),ip varchar(100),time datetime); Query OK, 0 rows affected (0.20 sec) mysql> mysql> desc ipinfo; +-----------+--------------+------+-----+---------+----------------+ | Field     | Type         | Null | Key | Default | Extra          | +-----------+--------------+------+-----+---------+----------------+ | id        | int(11)      | NO   | PRI | NULL    | auto_increment | | ipaddress | varchar(200) | YES  |     | NULL    |                | | countip   | int(11)      | YES  |     | NULL    |                | +-----------+--------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) mysql> desc whereip; +---------+--------------+------+-----+---------+----------------+ | Field   | Type         | Null | Key | Default | Extra          | +---------+--------------+------+-----+---------+----------------+ | id      | int(11)      | NO   | PRI | NULL    | auto_increment | | country | varchar(100) | YES  |     | NULL    |                | | area    | varchar(100) | YES  |     | NULL    |                | | region  | varchar(100) | YES  |     | NULL    |                | | city    | varchar(100) | YES  |     | NULL    |                | | ip      | varchar(100) | YES  |     | NULL    |                | | time    | datetime     | YES  |     | NULL    |                | +---------+--------------+------+-----+---------+----------------+ 7 rows in set (0.00 sec) mysql>

分析完日志查看數(shù)據(jù)庫的內(nèi)容:

mysql> select * from whereip limit 10; +----+---------+--------+-----------+-----------+-----------------+---------------------+ | id | country | area   | region    | city      | ip              | time                | +----+---------+--------+-----------+-----------+-----------------+---------------------+ |  1 | 美國    |        |           |           | 66.249.65.133   | 2016-09-01 10:53:01 | |  2 | 美國    |        |           |           | 23.251.63.45    | 2016-09-01 10:53:01 | |  3 | 美國    |        |           |           | 23.251.63.45    | 2016-09-01 10:53:01 | |  4 | 中國    | 華南   | 廣東省    | 廣州市    | 119.130.71.153  | 2016-09-01 10:53:01 | |  5 | 美國    |        |           |           | 66.249.65.183   | 2016-09-01 10:53:01 | |  6 | 美國    |        |           |           | 66.249.65.180   | 2016-09-01 10:53:01 | |  7 | 美國    |        |           |           | 66.249.65.142   | 2016-09-01 10:53:01 | |  8 | 美國    |        |           |           | 107.151.226.203 | 2016-09-01 10:53:01 | |  9 | 美國    |        |           |           | 107.151.226.203 | 2016-09-01 10:53:01 | | 10 | 美國    |        |           |           | 66.249.65.134   | 2016-09-01 10:53:01 | +----+---------+--------+-----------+-----------+-----------------+---------------------+ 10 rows in set (0.00 sec) mysql>

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

本文題目:python分析nginx日志的ip(來源)-創(chuàng)新互聯(lián)
鏈接分享:http://muchs.cn/article10/egpdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、微信公眾號(hào)、定制網(wǎng)站網(wǎng)站改版、靜態(tài)網(wǎng)站、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司