【Python】python練習
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=''
-
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python練習Python
- python 練習0000Python
- Python 練習題Python
- 新手練習:Python練習題目Python
- python練習小結Python
- python練習題解析Python
- Python 作業練習Python
- 五、python的練習題Python
- python相關練習題Python
- python練習 2020/10/13Python
- Python函式練習題Python函式
- Python基礎練習題Python
- Python學習-while迴圈練習PythonWhile
- python練習冊-第0000題Python
- python練習冊-第0002題Python
- python指令碼練習筆記Python指令碼筆記
- python006 列表練習題Python
- Python-100 練習題 02Python
- python 實現課堂練習Python
- python matplot繪圖工具練習Python繪圖
- python-購物車程式練習Python
- 05python的相關練習Python
- Python練習題篇(列表、字典、元祖)Python
- python 100題練習記錄(三)Python
- python 基礎之scrapy 原理練習Python
- python基礎 while迴圈練習PythonWhile
- python物件導向練習題01Python物件
- 9道python基礎練習題Python
- python基礎語句小練習Python
- 慕課網Python入門練習題---Python
- 練習安裝Python擴充套件庫Python套件
- python基礎(四)----列表、字典練習題Python
- Python面試的一些心得,與Python練習題分享Python面試
- Python入門必學,用Python練習畫個美隊盾牌Python
- 用Python如何統計文字檔案中的詞頻?(Python練習)Python
- Python練習01-對戰小遊戲Python遊戲
- python第一節課內容及練習Python
- Python3 (基礎練習)猴子吃桃Python
- 基礎練習——python特殊的數字——2020.11.17Python