为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

翻阅古今 2019-02-20 18:33:36
1.目前可以提取当天访问的ip 但是有bug第一个找到的不包含ip 还有无法在其中提取访问特定位置的记录。 2.代码` !/usr/bin/env python coding:utf-8 import urllibimport jsonimport timeimport 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"
查看完整描述

2 回答

?
慕村9548890

TA贡献1884条经验 获得超4个赞

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

查看完整回答
反对 回复 2019-03-01
?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

ip: 106.38.121.196 ,views: 1
ip: 180.168.174.128 ,views: 2
total: 2
max_ip_view: {'106.38.121.195': 2}

查看完整回答
反对 回复 2019-03-01
  • 2 回答
  • 0 关注
  • 636 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信