【Python】python练习

本文介绍如何使用Python进行日志文件的内容分析,包括统计命令执行次数及频率,并通过Python连接MySQL数据库进行数据检索与处理。此外,还提供了一个简单的Python脚本用于下载网页中的图片。
python练习:持续更新


》》》》》》》》》》》》》》》》》》》》》》》》》TEST1》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
需求:
  1. 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怎么实现呢

代码:
  1. [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》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
  1. 如下:
     #!/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() 

下载妹子图片
  1. # -*- coding: utf-8 -*-
  2. import urllib2
  3. import re
  4. import os
  5. url='http://www.meizitu.com'
  6. data=urllib2.urlopen(url).read()

  7. #获取本页的妹子图
  8. url2=re.findall('<img src="(.*)" alt=',data)
  9. print url2
  10. for i in url2:
  11.     name='D:/1111/'+re.sub(r'[/:-]','',i)
  12.     pag=urllib2.urlopen(i).read()
  13.     f=open(name,'wb')
  14.     f.write(pag)
  15.     f.close
  16.     print 'ok'

  17. ####获取本页下的超链接页的妹子图
  18. url3=re.findall('<a href="(.*.html)" ',data)
  19. #print url3
  20. for j in url3:
  21.     #print j
  22.     data2=urllib2.urlopen(j).read()
  23.     url4=re.findall('.*(http:.*.jpg).*',data2)
  24.     #print url4
  25.     for m in url4:
  26.         #print m
  27.         namee='D:/1111/'+re.sub(r'[/:-]','',m)
  28.         #print namee
  29.         meitu=urllib2.urlopen(m).read()
  30.         n=open(namee,'wb')
  31.         n.write(meitu)
  32.         n.close
  33.         print 'ok'



来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29096438/viewspace-1982636/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29096438/viewspace-1982636/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值