python练习 根据日志中的ip和url排序
#!/usr/bin/env python
#coding:utf-8 def open_file(file_name):
res={}
with open(file_name) as f:
for line in f:
tmp=line.split(' ')
# print tmp
ip,url=tmp[0],tmp[6]
# print ip,url 以元组的形式存到列表中
res[(ip,url)]=res.get((ip,url),0)+1
return sorted(res.items(),key=lambda x:x[1],reverse=True)
#print open_file('log1.log') def get_htmlstr(arr):
#拼接字符串
tmp1='<tr><td>Num%s</td> <td>%s</td> <td>%s</td> <td>%s</td></tr>'
html_str='<table border="1px">'+tmp1%('名次','IP','URL','COUNT')
for index,value in enumerate(arr[:10]):
html_str+='<tr><td>Num%s</td> <td>%s</td> <td>%s</td> <td>%s</td></tr>' %(index,value[0][0],value[0][1],value[1])
html_str+='</table>'
return html_str
def write_html(file_name):
res=open_file(file_name)
with open('res2.html','w') as f:
f.write(get_htmlstr(res))
write_html('log1.log')

python练习 根据日志中的ip和url排序的更多相关文章
- Python统计日志中每个IP出现次数
介绍了Python统计日志中每个IP出现次数的方法,实例分析了Python基于正则表达式解析日志文件的相关技巧,需要的朋友可以参考下 本脚本可用于多种日志类型 #-*- coding:utf-8 -* ...
- 配置日志中显示IP
package com.demo.conf; import ch.qos.logback.classic.pattern.ClassicConverter; import ch.qos.logback ...
- logresolve - 解析Apache日志中的IP地址为主机名
logresolve是一个解析Apache访问日志中IP地址的后处理程序. 为了使对名称服务器的影响降到最低,logresolve拥有极为自主的内部散列表缓存, 使每个IP值仅仅在第一次从日志文件中读 ...
- 使用Python 统计nginx日志前十ip访问量并以柱状图显示
脚本内容: import matplotlib.pyplot as plt # nginx_file = '10.6.11.91_access.log-2018-12-27' ip = {} #筛选n ...
- How To:分析ORACLE监听日志中的IP信息
有时候需要分析出ORACLE日志监听中的IP信息,分享一个组合命令,Linux的shell下运行正常. grep "HOST=.*establish.*\* 0" listener ...
- Python习题-统计日志中访问次数超过限制的IP
#1.1分钟之内ip访问次数超过200次的,就给他的ip加入黑名单#需求分析: #1.读日志,1分钟读一次 #2.获取这1分钟之内所有访问的ip #3.判断ip出现的次数,如果出现200次,那么就加入 ...
- python在json文件中提取IP和域名
# qianxiao996精心制作 #博客地址:https://blog.csdn.net/qq_36374896 import re def openjson(path): f = open(pat ...
- python先序、中序、后序排序
#encoding=utf-8 class Tree(): def __init__(self,leftjd=0,rightjd=0,data=0): self.leftjd = leftjd sel ...
- Excel中针对IP地址的排序方法
新建一个辅助排序列,用辅助列来扩展,辅助列公式如下: =TRIM(TEXT(LEFT(SUBSTITUTE(A1,".",REPT(" ",99)),100), ...
随机推荐
- [Angular 2] Directive intro and exportAs
First, What is directive, what is the difference between component and directive. For my understandi ...
- gradle使用小记
1.全局排除依赖: allprojects { apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'maven-p ...
- 更改gps.conf来加快GPS搜星速度
国内NTP ServerNTP全称是Network Time Protocol,是用来让计算机之间实现时间同步的协议,而发布这种校对时间的服务器,就是NTP Server!一般来说客户端与服务器之间的 ...
- 利用正则表达式作为string.split seprator
某字符串 var str = "{1,att,7},{2,break,7},{3,crit,7},{4,combo,7},{5,break,7},{6,hit,7}"; 需要分割成 ...
- memcache 存储session
php使用memcache存储session http://blog.csdn.net/weilee2009/article/details/7658260
- DataBase 之 常用操作
(1) try catch 配合 Transactions 使用 --打开try catch功能 set xact_abort on begin try begin tran ) commit tra ...
- C笔记01:关于printf函数输出先后顺序的讲解
关于printf函数输出先后顺序的讲解!! 对于printf函数printf("%d%d\n", a, b);函数的实际输出顺序是这样的先计算出b,然后再计算a,接着输出a,最后再 ...
- ArcGIS: version not specified. You must call RuntimeManager.Bind before creat
打开program.cs把ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);这句放到Applicatio ...
- ORACLE 导入导出操作
1.导入命令: imp userId/psw@orcl full=y file=D:\data\xxx.dmp ignore=y 2.导出命令 exp userId/psw@orcl file=d: ...
- python(1) -文件操作
很多时候我们需要对文件进行一些操作,比如读取并分析日志文件,写入日志文件等等.显然python也内置了对文件进行操作的函数. 读文件: >>> f = open('a.log','r ...