本文須要有必定的python和前端基礎,若是沒基礎的,請關注我後續的基礎教程系列博客
本文全部的demo,都是瀏覽器下展現的php
原創文章,本文同步發佈在github,跪求github右上角star前端
基於python,前端基於echarts,力求用簡單的代碼說明原理python
61.159.140.123 - - [23/Aug/2014:00:01:42 +0800] "GET /favicon.ico HTTP/1.1" 404 \ "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 LBBROWSER" "-" 61.159.140.123 - - [23/Aug/2014:00:01:42 +0800] "GET /favicon.ico HTTP/1.1" 404 \ "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 LBBROWSER" "-" 61.159.140.123 - - [23/Aug/2014:00:01:42 +0800] "GET /favicon.ico HTTP/1.1" 404 \ "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 LBBROWSER" "-" 61.159.140.123 - - [23/Aug/2014:00:01:42 +0800] "GET /favicon.ico HTTP/1.1" 404 \ "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 LBBROWSER" "-" 66.249.64.5 - - [23/Aug/2014:00:02:16 +0800] "GET /data/uploads/2013/0519/09/small_51982ba18e012.jpg HTTP/1.1" 200 \ "-" "Googlebot-Image/1.0" "-" 66.249.64.10 - - [23/Aug/2014:00:02:54 +0800] "GET /data/uploads/2013/0319/08/middle_5147b116e93b4.jpg HTTP/1.1" 200 \ "-" "Googlebot-Image/1.0" "-"
# coding=utf-8 f = open('www_access_20140823.log') res = {} for l in f: arr = l.split(' ') # 獲取ip url 和status ip = arr[0] url = arr[6] status = arr[8] # ip url 和status當key,每次統計+1 res[(ip,url,status)] = res.get((ip,url,status),0)+1 # 生成一個臨時的list res_list = [(k[0],k[1],k[2],v) for k,v in res.items()] # 按照統計數量排序,打印前10 for k in sorted(res_list,key=lambda x:x[3],reverse=True)[:10]: print k
('222.86.153.12', '/images/cursor_minify.cur', '404', 60) ('222.86.153.12', '/images/cursor_zoom.cur', '404', 32) ('58.253.6.133', '/images/cursor_minify.cur', '404', 32) ('111.85.34.165', '/%3Ca%20href=', '404', 28) ('58.253.6.133', '/images/cursor_zoom.cur', '404', 27) ('218.29.111.117', '/images/cursor_zoom.cur', '404', 27) ('218.29.111.117', '/images/cursor_minify.cur', '404', 26) ('117.63.146.40', '/public/js/common.js?20110824', '200', 19) ('117.63.146.40', '/favicon.ico', '404', 18) ('117.63.146.40', '/public/js/weibo.js?20110824', '200', 16)
生成list以後,拼接sql,存入數據庫mysql
import MySQLdb as mysql con = mysql.connect(user='root',\ passwd='',\ db='log',\ host='localhost') con.autocommit(True) cur = con.cursor() # 處理文件省略 for s in res_list: sql = 'insert log values ("%s","%s",%s,%s)' % s try: # 入庫 cur.execute(sql) except Exception, e: pass
from flask import Flask,request,render_template app = Flask(__name__) import MySQLdb as mysql con = mysql.connect(user='xx',\ passwd='xx',\ db='xx') cur = con.cursor() @app.route('/') def index(): table = '<table border="1">' cur.execute('select * from log order by value desc limit 20; ') for c in cur.fetchall(): table += '<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>'%c table +='</table>' return table if __name__ == '__main__': app.run(host='0.0.0.0',port=9092)
select status,sum(value) from log group by status +--------+------------+ | status | sum(value) | +--------+------------+ | 200 | 15529 | | 206 | 6 | | 301 | 2 | | 304 | 3549 | | 403 | 1 | | 404 | 847 | +--------+------------+ 6 rows in set (0.02 sec)
import urllib2 import json key = 'q5mTrTGzCSVq5QmGpI9y18Bo' ipurl = 'http://api.map.baidu.com/location/ip?ak='+key+'&coor=bd09ll&ip=' sqlarr = [] def getGeo(ip): try: u = urllib2.urlopen(ipurl+ip) page = json.load(u) if 'content' in page: point = page['content'].get('point') print 'ip %s has geoX %s and geoY %s' % (ip,point['x'],point['y']) except: print 'error' getGeo('202.198.16.3') # ip 202.198.16.3 has geoX 125.31364243 and geoY 43.89833761
原創文章,本文同步發佈在github 繼續求stargit