【Python】python練習

小亮520cl發表於2016-01-26
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=''
  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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章