Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1862398
  • 博文数量: 117
  • 博客积分: 2559
  • 博客等级: 少校
  • 技术积分: 4385
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-13 20:08
个人简介

作为初学者,要想取得进步,成为高手,首先应该了解自己的不足之处.

文章分类

全部博文(117)

文章存档

2014年(1)

2013年(25)

2012年(13)

2011年(77)

2010年(1)

分类: 系统运维

2011-01-20 14:45:39

  1. #!/usr/bin/python
  2. #-*- coding: UTF-8 -*-

  3. import MySQLdb,cgi,time,datetime
  4.     
  5. today = datetime.datetime.now()
  6. today_0 = today.strftime('%Y-%m-%d')
  7. today_1 = (today + datetime.timedelta(days = -1)).strftime('%Y-%m-%d')
  8. today_2 = (today + datetime.timedelta(days = -2)).strftime('%Y-%m-%d')
  9. today_3 = (today + datetime.timedelta(days = -3)).strftime('%Y-%m-%d')
  10. today_4 = (today + datetime.timedelta(days = -4)).strftime('%Y-%m-%d')
  11. today_5 = (today + datetime.timedelta(days = -5)).strftime('%Y-%m-%d')
  12. today_6 = (today + datetime.timedelta(days = -6)).strftime('%Y-%m-%d')


  13. header = 'Content-Type: text/html\n\n'
  14. url = '/cgi-bin/test_web2.py'
  15.     
  16. formhtml = '''<br /> </span> </li> <li> rsync_backup
  17. The Backup List: %s



  18. Please Input Module Name:


  19. Please enter the date(custom):


  20. Please enter the date:

  21. %s
  22. '''

  23.     
  24. fradio = ' %s\n'
  25.     
  26. def showForm(who, howmany):
  27.     friends = ''
  28.     for i in [today_0,today_1,today_2,today_3,today_4,today_5,today_6]:
  29.         checked = ''
  30.         if str(i) == howmany:
  31.             checked = 'CHECKED'
  32.         friends = friends + fradio % (str(i), checked, str(i))
  33.     print header + formhtml % (who, url, who, howmany, friends)
  34.     
  35. reshtml_01 = '''
  36. rsync_backup
  37. The Backup List: %s


  38. Your name is: %s


  39. The current date: %s
  40. Click here to edit your data again.


  41.   

  42.     

  43.     

  44.     

  45.     

  46.     

  47.     

  48.     

  49.     

  50.     

  51.     

  52.   

  53. '''
  54. reshtml_02 = '''
  55.   

  56.     

  57.     

  58.     

  59.     

  60.     

  61.     

  62.     

  63.     

  64.     

  65.     

  66.   

  67. '''
  68. def doResults(who, howmany, today_now):
  69.     n=1
  70.     print header + reshtml_01 % (who, who, howmany, url)
  71.     db_conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='test')
  72.     db_curs=db_conn.cursor()
  73.     if who == '':
  74.         db_curs.execute("SELECT id,date_ymd,s_time,e_time,mod_name,files,files_traned,filesize,filesize_traned,a_error \
  75.                          FROM bak_stat where date_ymd='%s' " % today_now )
  76.         for data in db_curs.fetchall():
  77.             if data[9] == '':
  78.                 print reshtml_02 % (n,data[1],data[2],data[3],data[4], \
  79.                                     format(data[5],','),format(data[6],','),format(data[7],','),format(data[8],','),data[9])
  80.             else:
  81.                 print reshtml_02 % (n,data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9])
  82.             n+=1
  83.     else:
  84.         db_curs.execute("SELECT id,date_ymd,s_time,e_time,mod_name,files,files_traned,filesize,filesize_traned,a_error \
  85.                          FROM bak_stat where mod_name='%s' " % who )
  86.         for data in db_curs.fetchall():
  87.             if data[9] == '':
  88.                 print reshtml_02 % (n,data[1],data[2],data[3],data[4], \
  89.                                     format(data[5],','),format(data[6],','),format(data[7],','),format(data[8],','),data[9])
  90.             else:
  91.                 print reshtml_02 % (n,data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9])
  92.             n+=1
  93.     db_curs.close()
  94.     db_conn.close()
  95. def process():
  96.     error = ''
  97.     form = cgi.FieldStorage()
  98.     
  99.     if form.has_key('person'):
  100.         who = form['person'].value
  101.     else:
  102.         who = ''
  103.     
  104.     if form.has_key('howmany'):
  105.         howmany = form['howmany'].value
  106.     else:
  107.         howmany = ''
  108.     
  109.     if form.has_key('action'):
  110.         doResults(who, howmany,howmany)
  111.     else:
  112.         showForm(who, howmany)

  113. if __name__ == '__main__':
  114.     process()
  115.  

    部分代码解释:

    #!/usr/bin/python
    #-*- coding: UTF-8 -*-
    编码改成UTF-8,避免中文字符不能输出。

    import MySQLdb,cgi,time,datetime
    #time,datetime模块

    today = datetime.datetime.now()
    today_0 = today.strftime('%Y-%m-%d')
    today_1 = (today + datetime.timedelta(days = -1)).strftime('%Y-%m-%d')
    today_2 = (today + datetime.timedelta(days = -2)).strftime('%Y-%m-%d')
    today_3 = (today + datetime.timedelta(days = -3)).strftime('%Y-%m-%d')
    today_4 = (today + datetime.timedelta(days = -4)).strftime('%Y-%m-%d')
    today_5 = (today + datetime.timedelta(days = -5)).strftime('%Y-%m-%d')
    today_6 = (today + datetime.timedelta(days = -6)).strftime('%Y-%m-%d')
    #定义最近一周的时间

    header = 'Content-Type: text/html\n\n'
    url = '/cgi-bin/test_web2.py'
    #定义html头文件和url

    formhtml = '''<br /> rsync_backup

    The Backup List: %s



    Please Input Module Name:

    Please enter the date(custom):

    Please enter the date:
    %s

    '''
    #定义首页
    #action 处的“hidden”变量,这里的值为“edit”。决定显示哪个页面(表单页面或是结果页面)
    #可以查看url路径值验证。

    fradio = ' %s\n'
    def showForm(who, howmany):
        friends = ''
        for i in [today_0,today_1,today_2,today_3,today_4,today_5,today_6]:
            checked = ''
            if str(i) == howmany:
                checked = 'CHECKED'
            friends = friends + fradio % (str(i), checked, str(i))
        print header + formhtml % (who, url, who, howmany, friends)
    #操作html按钮的函数
    #变量fradio,用来创建单选按钮的字符串

    reshtml_01 = '''
    rsync_backup

    The Backup List: %s


    Your name is: %s


    The current date: %s

    Click here to edit your data again.

    ID号 日期 开始时间 结束时间 模块名 总文件数 传输文件数 总文件大小 传输文件大小 错误备注
    %s %s %s %s %s %s %s %s %s %s

     
       
       
       
       
       
       
       
       
       
       
     
    '''
    #定义一个表头
    #style="border:black solid 1px;border-collapse:collapse"的作用是把边框设置为1px

    reshtml_02 = '''
     


       
       
       
       
       
       
       
       
       
       
     
    '''   
    #定义每一行的格式,nowrap="nowrap"的意思是,内容不自动换行。

    def doResults(who, howmany, today_now):
        n=1
        print header + reshtml_01 % (who, who, howmany, url)
    #首先打印头文件和reshtml_01页面
        db_conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='test')
        db_curs=db_conn.cursor()
    #创建数据库连接和游标
        if who == '':
            db_curs.execute("SELECT id,date_ymd,s_time,e_time,mod_name,files,files_traned,filesize,filesize_traned,a_error \
                             FROM bak_stat where date_ymd='%s' " % today_now )
            for data in db_curs.fetchall():
                if data[9] == '':
                    print reshtml_02 % (0,data[1],data[2],data[3],data[4], \
                                        format(data[5],','),format(data[6],','),format(data[7],','),format(data[8],','),data[9])
                else:
                    print reshtml_02 % (0,data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9])
                n+=1
    #设置一个ID编号,从1开始,统计当天的备份计数,如果读data[0],则是数据库总的计数。
        else:
            db_curs.execute("SELECT id,date_ymd,s_time,e_time,mod_name,files,files_traned,filesize,filesize_traned,a_error \
                             FROM bak_stat where mod_name='%s' " % who )
            for data in db_curs.fetchall():
                if data[9] == '':
                    print reshtml_02 % (n,data[1],data[2],data[3],data[4], \
                                        format(data[5],','),format(data[6],','),format(data[7],','),format(data[8],','),data[9])
                else:
                    print reshtml_02 % (n,data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9])
                n+=1
    #format为格式化千分位分隔符,但是测试用的python2.7版本,因为在python2.4中,还没有加入format这个内置函数。
    #data是形如 (2L, datetime.date(2010, 11, 23), datetime.timedelta(0, 36866), datetime.timedelta(0, 36866), 'mytest1', 3L, 0L, 17696256L, 0L, '') 这样的一行数组。L代表长整型。
    #判断,如果who变量为空,则执行……,否则
        db_curs.close()
        db_conn.close()
    def process():
        error = ''
        form = cgi.FieldStorage()
       
        if form.has_key('person'):
            who = form['person'].value
        else:
            who = ''
        if form.has_key('howmany'):
            howmany = form['howmany'].value
        else:
            howmany = ''
        if form.has_key('action'):
            doResults(who, howmany,howmany)
        else:
            showForm(who, howmany)
    #重点:
    #1·FieldStorage是cgi中的主要函数,它完成所有工作。
    #2·form = cgi.FieldStorage()表示实例化了这个类。之后,会从浏览器读取输入的信息。
    #3·被实例化后,包含一个类似字典的对象,具有一系列的键-值对,键就是通过表单传入的表单条目的名字,而值则包含相应的数据。
    #4·说明,person——键,form['person'].value——值
    #5·如果form.has_key('action'),action键存在,就执行doResults语句

    if __name__ == '__main__':
        process()

    阅读(2491) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~
    ID号日期开始时间结束时间模块名总文件数传输文件数总文件大小(byte)传输文件大小(byte)错误备注
    %s%s%s%s%s%s%s%s%s%s