python2脚本如何提取nginx日志当天访问特定位置的ip

1.目前可以提取当天访问的ip 但是有bug第一个找到的不包含ip 还有无法在其中提取访问特定位置的记录。


2.代码`

!/usr/bin/env python

coding:utf-8

import urllib
import json
import time
import re

ld = time.strftime("%d/%b/%Y", time.localtime())
url = 'http://ip.taobao.com/service/...'

def ip_find(ip):

data = urllib.urlopen(url + ip).read()
datadict = json.loads(data)
for oneinfo in datadict:
    if 'code' == oneinfo:
        if datadict[oneinfo] == 0:
            return datadict['data']['country'] + datadict['data']['region'] + datadict['data']['city'] + '\t' + datadict['data']['isp']

def sort_value(s):

d = sorted(s.iteritems(),key=lambda t:t[1],reverse=True)
return d

if name == '__main__':

with open('access.log') as f:
    content = f.read()
    patt = re.compile(ld + r'(.*)', re.S)
    result = re.search(patt, content).group(1)
file = r'test.txt'
with open(file, 'w+') as f:
    f.write(result)

with open('test.txt') as f: # /opt/nginx/logs/
    d = {}
    for line in f:
        field = line.split()
        print field
        if field[0] not in d:
            d.setdefault(field[0],[])
        d[field[0]].append(field[0])

    s = {}
    for k in d:
        s[k] = len(d[k])
    s = sort_value(s)[0:10]
    print '访问IP\t\t\t访问次数\t\tIP来源'
    print '----------------------------------------------------------------------------'
    for ip,con in s:
        print str(ip) + '\t\t' + str(con) + '\t\t' + ip_find(ip)`

3.日志文件
106.38.121.196 - - [08/Jun/2018:18:15:43 +0800] "POST /supervision/api/user/login.do HTTP/1.1" 200 503 "http://supervision.bangcle.com/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36"
180.168.174.128 - - [11/Jun/2018:09:12:04 +0800] "POST /supervision/api/user/login.do HTTP/1.1" 200 491 "http://supervision.bangcle.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36"
106.38.121.195 - - [11/Jun/2018:18:11:04 +0800] "GET /static/js/vendor.180eb0f8247b996979d3.js HTTP/1.1" 304 0 "http://supervision.bangcle.com/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36"

翻阅古今
浏览 637回答 2
2回答

慕村9548890

可以采用result = re.split(ld,content)[0].split('"')[0] + ld + re.search(patt, content).group(1)

繁星点点滴滴

ip: 106.38.121.196 ,views: 1ip: 180.168.174.128 ,views: 2total: 2max_ip_view: {'106.38.121.195': 2}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python