Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1781445
  • 博文数量: 276
  • 博客积分: 1574
  • 博客等级: 上尉
  • 技术积分: 2894
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-26 23:23
个人简介

生活的美妙在于,不知道一下秒是惊艳还是伤神,时光流转,珍惜现在的拥有的时光

文章分类

全部博文(276)

文章存档

2017年(17)

2016年(131)

2015年(63)

2013年(2)

2012年(32)

2011年(31)

分类: Python/Ruby

2016-05-25 11:51:53





t@localhost webapp$ tree
.
├── cgi-bin
│   ├── athletemodel.py
│   ├── generate_list.py
│   ├── generate_timing_data.py
│   ├── kelly_c.py
│   └── yate.py
├── coach.css
├── data
│   ├── athletes.pickle
│   ├── james.txt
│   ├── julie.txt
│   ├── mikey.txt
│   └── sarah.txt
├── favicon.ico
├── images
│   └── coach-head.jpg
├── index.html
├── simple_httpd.py
└── templates
    ├── footer.html
    └── header.html


4 directories, 17 files


点击(此处)折叠或打开

  1. -rw-r--r--. 1 t t 263 5月 25 10:17 ./simple_httpd.py
  2. #!/usr/bin/env python3
  3. # -*- coding:utf-8 -*-


  4. from http.server import HTTPServer, CGIHTTPRequestHandler

  5. port = 8080

  6. httpd = HTTPServer(('', port), CGIHTTPRequestHandler)
  7. print("Starting simple_httpd on port: " + str(httpd.server_port))
  8. httpd.serve_forever()



  1. -rwxrwxr-x. 1 t t 672 5月 25 10:34 ./cgi-bin/generate_list.py
  2. #!/usr/bin/env python3
  3. # -*- coding:utf-8 -*-

  4. #导入M,V
  5. import athletemodel, yate
  6. #glob 模块可以向操作系统查询一个文件名列表
  7. import glob

  8. #生成一个选择运动员列表html页面
  9. data_files = glob.glob('data/*.txt')
  10. athletes = athletemodel.put_to_store(data_files)

  11. print(yate.start_response())
  12. print(yate.include_header("kelly教练的运动员列表"))

  13. print(yate.start_form("generate_timing_data.py"))
  14. print(yate.para("从列表中选择一个运动员:"))
  15. for each_athlete in athletes:
  16.     print(yate.radio_button("which_athlete",athletes[each_athlete].name))
  17. print(yate.end_form("Select"))


  18. print(yate.include_footer({"Home":"/index.html"}))














  1. -rwxrwxr-x. 1 t t 746 5月 25 11:33 ./cgi-bin/generate_timing_data.py
  2. #!/usr/bin/env python3
  3. # -*- coding:utf-8 -*-

  4. #使用cgi模块处理表单数据
  5. import cgi
  6. #cgi跟踪模块
  7. import cgitb
  8. cgitb.enable()
  9. #将所有表单数据放在一个字典中
  10. form_data = cgi.FieldStorage()
  11. athlete_name = form_data['which_athlete'].value

  12. import athletemodel,yate
  13. #取出pickle数据
  14. athletes = athletemodel.get_from_store()

  15. #生成运动员时间显示页面
  16. print(yate.start_response())
  17. print(yate.include_header("时间数据信息"))
  18. print(yate.header("运动员:" + athlete_name + ", 出生日期:" + athletes[athlete_name].dob + "."))
  19. print(yate.para("最佳三次成绩为:"))
  20. print(yate.u_list(athletes[athlete_name].top3))
  21. print(yate.include_footer({"Home":"/index.html","其他成员数据":"generate_list.py"}))





  1. -rwxr-xr-x. 1 t t 1511 5月 25 10:24 ./cgi-bin/yate.py
  2. #从string模块中导入类,支持简单的字符串替换模板.
  3. from string import Template

  4. #生成文件类型
  5. def start_response(resp="text/html"):
  6.     return('Content-type: ' + resp + ';charset=utf-8\n\n')

  7. #
  8. def include_header(the_title):
  9.     with open('templates/header.html') as headf:
  10.         head_text = headf.read()
  11.     header = Template(head_text)
  12.     return(header.substitute(title=the_title))

  13. def include_footer(the_links):
  14.     with open('templates/footer.html') as footf:
  15.         foot_text = footf.read()
  16.     link_string = ''
  17.     for key in the_links:
  18.         link_string += '+ the_links[key] + '">' + key + '    '
  19.     footer = Template(foot_text)
  20.     return(footer.substitute(links=link_string))

  21. def start_form(the_url, form_type="POST"):
  22.     return('
    + the_url + '" method="' + form_type + '">')

  23. def end_form(submit_msg="Submit"):
  24.     return('


    +
    submit_msg + '">')

  25. def radio_button(rb_name, rb_value):
  26.     return('+ rb_name +
  27.                              '" value="' + rb_value + '"> ' + rb_value + '
    '
    )

  28. def u_list(items):
  29.     u_string = '
      '
    •     for item in items:
    •         u_string += '
    • ' + item + '
    • '
    •     u_string += '
    '

  30.     return(u_string)

  31. def header(header_text, header_level=2):
  32.     return('(header_level) + '>' + header_text +
  33.            ' + str(header_level) + '>')

  34. def para(para_text):
  35.     return('

    ' + para_text + '

    '
    )



  1. -rwxr-xr-x. 1 t t 2086 5月 25 11:30 ./cgi-bin/athletemodel.py
  2. #!/usr/bin/evn python3
  3. # -*- coding:utf8 -*-
  4. '''
  5. 1.读取文件 => put_to_store => pickle
  6. 2.pickle => get_from_store => viewer
  7. '''
  8. import pickle
  9. from kelly_c import athletelist
  10. #磁盘文件处理
  11. def openfile(filename):
  12.     try:
  13.         #打开文件
  14.         with open(filename) as athlete_file:
  15.             #读取数据
  16.             data = athlete_file.readline()
  17.             #初步处理数据,去空,,号分割
  18.             value_list= data.strip().split(',')
  19.             #分别取出有格式的三种数据
  20.             username = value_list.pop(0)
  21.             userdob = value_list.pop(0)
  22.             usertimes= value_list
  23.             #返回实例对象
  24.             athlete_instance=athletelist(username,userdob,usertimes)
  25.             return(athlete_instance)
  26.     except IOError as ioerr:
  27.         print('File error %s' % ioerr)
  28.         return(None)

  29. #内容压制,使用字典数据类型.
  30. def put_to_store(files_list):
  31.     #字典生成
  32.     all_athletes = {}
  33.     for each_file in files_list:
  34.         each_athlete = openfile(each_file)
  35.         all_athletes[each_athlete.name] = each_athlete
  36.     #pickle数据压制
  37.     try:
  38.         with open('data/athletes.pickle','wb') as athlfile:
  39.             pickle.dump(all_athletes,athlfile)
  40.     except IOError as ioerr:
  41.         print('File error(%s)' % ioerr)
  42.     return(all_athletes)

  43. def get_from_store():
  44.     all_athletes = {}
  45.     #pickle数据解压
  46.     try:
  47.         with open('data/athletes.pickle','rb') as athlfile:
  48.             all_athletes=pickle.load(athlfile)
  49.     except IOError as ioerr:
  50.         print('File error(%s)' % ioerr)
  51.     return(all_athletes)

  52. #files_list = ["../data/james.txt", "../data/julie.txt", "../data/mikey.txt", "../data/sarah.txt"]
  53. #data = put_to_store(files_list)
  54. #test
  55. '''
  56. print(get_from_store())
  57. print(dir())
  58. type(data)
  59. print('Use put_to_store()')
  60. for each_athlete in data:
  61.     print(data[each_athlete].name,data[each_athlete].dob)
  62. print('Use get_from_store()')
  63. data_copy = get_from_store()
  64. for each_athlete in data_copy:
  65.     print(data_copy[each_athlete].name,data_copy[each_athlete].dob)
  66. '''





  1. -rwxrwxr-x. 1 t t 605 5月 25 11:33 ./cgi-bin/kelly_c.py
  2. #!/usr/bin/env python3
  3. # -*- coding:utf-8 -*-
  4. class athletelist(list):
  5.     def __init__(self, a_name, a_dob=None, a_times=[]):
  6.         list.__init__([])
  7.         self.name = a_name
  8.         self.dob = a_dob
  9.         self.extend(a_times)
  10.     @property
  11.     def top3(self):
  12.         return(sorted(set([sanitize(t) for t in self]))[0:3])

  13. #处理字符,转换成m.s格式
  14. def sanitize(time_string):
  15.     if '-' in time_string:
  16.         splitter = '-'
  17.     elif ':' in time_string:
  18.         splitter = ':'
  19.     else:
  20.         return time_string
  21.     (min, sec) = time_string.split(splitter)
  22.     return (min + '.' + sec)
-rw-r--r--. 1 t t 84 7月  24 2010 ./data/mikey.txt
Mikey McManus,2002-2-24,2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38,2:40,2.22,2-31
-rw-r--r--. 1 t t 82 7月  25 2010 ./data/julie.txt
Julie Jones,2002-8-17,2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21,3.01,3.02,2:59
-rw-r--r--. 1 t t 80 8月  29 2010 ./data/james.txt
James Lee,2002-3-14,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22,2-01,2.01,2:16
-rw-r--r--. 1 t t 84 7月  25 2010 ./data/sarah.txt
Sarah Sweeney,2002-6-17,2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55,2:22,2-21,2.22

t@localhost webapp$ find . -name '*.html' -exec ls -l  {} \; -exec cat {} \;   

阅读(2323) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

且行且忆2017-11-15 10:34:37

楼主这些代码从何而来,我在python那个网站上下载下来的,缺少了好几个文件