python分析nginx日誌的ip(上篇)

很早以前就想寫個有這樣功能的程序了。。。。。。php

直到在一個漆黑的夜裏。。。。。。。看了一個大姐寫了統計ip歸屬地的代碼。。。。。。。python

個人日誌是天天輪詢一次,生成新的log,因此個人數據庫中的時間信息,記錄的是我代碼執行時插入的時間。mysql

代碼執行結果以下:                                           sql

數據庫中的信息以下:數據庫

數據樣本:日誌中的格式大概以下json

代碼以下api

#!/usr/bin env python
# coding: utf-8
import MySQLdb as mysql
import datetime
import sys, os, urllib2, jsonapp

db = mysql.connect(user="root",passwd="123456",db="intest",host="localhost") #數據庫鏈接信息
db.autocommit(True)
cur = db.cursor()
cur.execute('set names utf8')
addtime = datetime.datetime.now()url

ips = {}  #ip做爲字典的key,訪問次數作value
iplist = [] #遍歷日誌中的ip,相同的ip也會記錄到列表,插入數據庫
fh = open("/root/king/03.log", "r").readlines()                                                               #個人是把日誌和代碼在一個目錄下面
for line in fh:
    ip = line.split(" ")[0]
    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 'ok'
  else:
   print(data)
 except Exception as ex:
  print(ex)

if __name__ == '__main__':
 for ip in iplist:
     get_ip_area(ip)

數據庫中的建表語句,和表結構以下。

create table ipinfo(id int auto_increment primary key,ipaddress varchar(200),countip int);

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);

相關文章
相關標籤/搜索