python练习:持续更新
》》》》》》》》》》》》》》》》》》》》》》》》》TEST1》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
需求:
代码:
[root@host-192-168-2-177 ~]# python cou.py
test/robot/r4.c 4
test/robot/r5.c 2
test/robot/login_helper.c 9
system 1
test/robot/robot_cfg.h 2
shell版本(一条命令,还是比较方便的,但是当日志量大的时候用python的效率会高些):
[root@host-192-168-2-177 ~]# more http.log |awk -F " " '{print $3}'|sort| sed '/^$/d'|uniq -c
1 system
9 test/robot/login_helper.c
4 test/robot/r4.c
2 test/robot/r5.c
2 test/robot/robot_cfg.h
》》》》》》》》》》》》》》》》》》》》》》》》》TEST2》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
下载妹子图片
》》》》》》》》》》》》》》》》》》》》》》》》》TEST1》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
需求:
-
2015-11-26 16:54:58 system is be initialized: interval=[500000]us
2015-11-26 16:54:59 test/robot/robot_cfg.h 50:arrArgs = [
"192.168.12.33:2500",
"4",
"100",
"60",
"5",
"60"
]
2015-11-26 16:54:59 test/robot/robot_cfg.h 50:arrArgs = [
"192.168.12.33:2500",
"7",
"100",
"60",
"5",
"60"
]
2015-11-26 16:55:03 test/robot/login_helper.c 174:寻绿82420097 random grade:30
2015-11-26 16:55:03 test/robot/login_helper.c 174:又绿82420100 random grade:60
2015-11-26 16:55:03 test/robot/login_helper.c 174:尔岚82420103 random grade:50
2015-11-26 16:55:03 test/robot/login_helper.c 174:秋柏82420106 random grade:60
2015-11-26 16:55:03 test/robot/login_helper.c 174:乐菱82420109 random grade:20
2015-11-26 16:55:03 test/robot/login_helper.c 174:巧曼82420112 random grade:30
2015-11-26 16:55:03 test/robot/login_helper.c 174:念芹82420115 random grade:60
2015-11-26 16:55:03 test/robot/login_helper.c 174:曼香82420118 random grade:30
2015-11-26 16:55:03 test/robot/login_helper.c 174:平绿82420121 random grade:30
2015-11-26 16:55:04 test/robot/r5.c 32:uid=7037284, logined
2015-11-26 16:55:04 test/robot/r5.c 32:uid=7037285, logined
2015-11-26 16:55:04 test/robot/r4.c 29:uid=7037286, logined
2015-11-26 16:55:04 test/robot/r4.c 29:uid=7037283, logined
2015-11-26 16:55:04 test/robot/r4.c 29:uid=7037287, logined
2015-11-26 16:55:04 test/robot/r4.c 29:uid=7037288, logined
这是日志一部分 就是要统计每隔十分钟 命令的种类和对应执行过的次数 用python怎么实现呢
代码:
-
[root@host-192-168-2-177 ~]# vi cou.py
#!/usr/bin/python
import re
import fileinput
dics={}
for line in fileinput.input('http.log'):
if line.find('2015')>=0:
Lists=line.split()
# print Lists
if Lists[2] not in dics:
dics[Lists[2]]=1
else:
dics[Lists[2]]+=1
for key,value in dics.items():
print key,value
[root@host-192-168-2-177 ~]# python cou.py
test/robot/r4.c 4
test/robot/r5.c 2
test/robot/login_helper.c 9
system 1
test/robot/robot_cfg.h 2
shell版本(一条命令,还是比较方便的,但是当日志量大的时候用python的效率会高些):
[root@host-192-168-2-177 ~]# more http.log |awk -F " " '{print $3}'|sort| sed '/^$/d'|uniq -c
1 system
9 test/robot/login_helper.c
4 test/robot/r4.c
2 test/robot/r5.c
2 test/robot/robot_cfg.h
》》》》》》》》》》》》》》》》》》》》》》》》》TEST2》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
-
如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import MySQLdb,os
conn=MySQLdb.connect(user="root",passwd="XXXXXXXXX",db="interface_hd_com",port=3307,host="127.0.0.1",charset="utf8")
cursor =conn.cursor()
def get_all_uid():
v_sql ="select distinct(uid) FROM b_citywide_peer limit 10"
cursor.execute(v_sql)
row=cursor.fetchall()
return row
def get_name(x):
v_sql ="select name FROM b_users where uid= %d" % x
cursor.execute(v_sql)
row=cursor.fetchall()
return row[0][0]
def get_tel(x):
v_sql ="select tel FROM b_users where uid= %d" % x
cursor.execute(v_sql)
row=cursor.fetchall()
return row[0][0]
def get_name_tel(x):
v_sql ="select name,tel FROM b_users where uid= %d" % x
cursor.execute(v_sql)
row=cursor.fetchall()
return row
def get_my_foc(id):
sql ="select uid ,group_concat(c_uid) FROM b_citywide_peer group by uid having uid=%d" % id
cursor.execute(sql)
row=cursor.fetchall()
row2=row[0]
my_foc=set(row2[1].split(','))
return my_foc
def get_foc_me(id):
sql2="select c_uid,group_concat(uid) from b_citywide_peer group by c_uid having c_uid= %d" % id
cursor.execute(sql2)
row=cursor.fetchall()
print row
row2=row[0]
foc_me=set(row2[1].split(','))
return foc_me
def save_to_txt(path,data):
file_path = os.path.split(path)
if not os.path.exists( file_path[0] ):
os.makedirs( file_path[0] )
print '创建目录成功'
if len(data)>0:
try:
with open( path.encode('utf-8'),'ab' ) as f:
data = data+"\n"
f.write( data.encode('utf-8') )
except:
print '数据插入失败'
else:
print '没有数据'
def get_erdu_link():
for m in get_all_uid():
print m
uid=m[0]
Sec_fid=get_foc_me(uid).difference(get_my_foc(uid))
for i in Sec_fid:
j=int(i)
lists=get_name_tel(j)
erdu_name=lists[0][0]
erdu_tel=lists[0][1]
self_name=get_name(uid)
self_tel=get_tel(uid)
str= "%s %s %s %s %s" % (self_name,self_tel,u"二度人脉",erdu_name,erdu_tel)
print str
save_to_txt("./tt.log",str)
#print get_foc_me(140)
#print get_all_uid()
get_erdu_link()
cursor.close()
conn.close()
下载妹子图片
-
# -*- coding: utf-8 -*-
-
import urllib2
-
import re
-
import os
-
url='http://www.meizitu.com'
-
data=urllib2.urlopen(url).read()
-
-
#获取本页的妹子图
-
url2=re.findall('<img src="(.*)" alt=',data)
-
print url2
-
for i in url2:
-
name='D:/1111/'+re.sub(r'[/:-]','',i)
-
pag=urllib2.urlopen(i).read()
-
f=open(name,'wb')
-
f.write(pag)
-
f.close
-
print 'ok'
-
-
####获取本页下的超链接页的妹子图
-
url3=re.findall('<a href="(.*.html)" ',data)
-
#print url3
-
for j in url3:
-
#print j
-
data2=urllib2.urlopen(j).read()
-
url4=re.findall('.*(http:.*.jpg).*',data2)
-
#print url4
-
for m in url4:
-
#print m
-
namee='D:/1111/'+re.sub(r'[/:-]','',m)
-
#print namee
-
meitu=urllib2.urlopen(m).read()
-
n=open(namee,'wb')
-
n.write(meitu)
-
n.close
- print 'ok'
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29096438/viewspace-1982636/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29096438/viewspace-1982636/
本文介绍如何使用Python进行日志文件的内容分析,包括统计命令执行次数及频率,并通过Python连接MySQL数据库进行数据检索与处理。此外,还提供了一个简单的Python脚本用于下载网页中的图片。
2566

被折叠的 条评论
为什么被折叠?



