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
- rw- r- - r- - . 1 t t 263 5月 25 10: 17 . / simple_httpd. py
#! / usr/ bin/ env python3
# - * - coding: utf- 8 - * -
from http. server import HTTPServer, CGIHTTPRequestHandler
port = 8080
httpd = HTTPServer( ( '' , port) , CGIHTTPRequestHandler)
print ( "Starting simple_httpd on port: " + str( httpd. server_port) )
httpd. serve_forever( )
- rwxrwxr- x. 1 t t 672 5月 25 10: 34 . / cgi- bin/ generate_list. py
#! / usr/ bin/ env python3
# - * - coding: utf- 8 - * -
#导入M, V
import athletemodel, yate
#glob 模块可以向操作系统查询一个文件名列表
import glob
#生成一个选择运动员列表html页面
data_files = glob. glob( 'data/*.txt' )
athletes = athletemodel. put_to_store( data_files)
print ( yate. start_response( ) )
print ( yate. include_header( "kelly教练的运动员列表" ) )
print ( yate. start_form( "generate_timing_data.py" ) )
print ( yate. para( "从列表中选择一个运动员:" ) )
for each_athlete in athletes:
print ( yate. radio_button( "which_athlete" , athletes[ each_athlete] . name) )
print ( yate. end_form( "Select" ) )
print ( yate. include_footer( { "Home" : "/index.html" } ) )
- rwxrwxr- x. 1 t t 746 5月 25 11: 33 . / cgi- bin/ generate_timing_data. py
#! / usr/ bin/ env python3
# - * - coding: utf- 8 - * -
#使用cgi模块处理表单数据
import cgi
#cgi跟踪模块
import cgitb
cgitb. enable( )
#将所有表单数据放在一个字典中
form_data = cgi. FieldStorage( )
athlete_name = form_data[ 'which_athlete' ] . value
import athletemodel, yate
#取出pickle数据
athletes = athletemodel. get_from_store( )
#生成运动员时间显示页面
print ( yate. start_response( ) )
print ( yate. include_header( "时间数据信息" ) )
print ( yate. header( "运动员:" + athlete_name + ", 出生日期:" + athletes[ athlete_name] . dob + "." ) )
print ( yate. para( "最佳三次成绩为:" ) )
print ( yate. u_list( athletes[ athlete_name] . top3) )
print ( yate. include_footer( { "Home" : "/index.html" , "其他成员数据" : "generate_list.py" } ) )
- rwxr- xr- x. 1 t t 1511 5月 25 10: 24 . / cgi- bin/ yate. py
#从string模块中导入类, 支持简单的字符串替换模板.
from string import Template
#生成文件类型
def start_response( resp= "text/html" ) :
return ( 'Content-type: ' + resp + ';charset=utf-8\n\n' )
#
def include_header( the_title) :
with open( 'templates/header.html' ) as headf:
head_text = headf. read( )
header = Template( head_text)
return ( header. substitute( title= the_title) )
def include_footer( the_links) :
with open( 'templates/footer.html' ) as footf:
foot_text = footf. read( )
link_string = ''
for key in the_links:
link_string + = '+ the_links[ key] + '">' + key + ' '
footer = Template( foot_text)
return ( footer. substitute( links= link_string) )
def start_form( the_url, form_type= "POST" ) :
return ( '
def end_form( submit_msg= "Submit" ) :
return ( '
+ submit_msg + '">' )
def radio_button( rb_name, rb_value) :
return ( ' + rb_name +
'" value="' + rb_value + '"> ' + rb_value + '
' )
def u_list( items) :
u_string = '
'
for item in items:
u_string + = '
' + item + '
'
u_string + = '
'
return ( u_string)
def header( header_text, header_level= 2) :
return ( '( header_level) + '>' + header_text +
' + str( header_level) + '>' )
def para( para_text) :
return ( '
' + para_text + '
' )
- rwxr- xr- x. 1 t t 2086 5月 25 11: 30 . / cgi- bin/ athletemodel. py
#! / usr/ bin/ evn python3
# - * - coding: utf8 - * -
'' '
1.读取文件 => put_to_store => pickle
2.pickle => get_from_store => viewer
'''
import pickle
from kelly_c import athletelist
#磁盘文件处理
def openfile( filename) :
try :
#打开文件
with open( filename) as athlete_file:
#读取数据
data = athlete_file. readline( )
#初步处理数据, 去空, 以, 号分割
value_list= data. strip( ) . split( ',' )
#分别取出有格式的三种数据
username = value_list. pop( 0)
userdob = value_list. pop( 0)
usertimes= value_list
#返回实例对象
athlete_instance= athletelist( username, userdob, usertimes)
return ( athlete_instance)
except IOError as ioerr:
print ( 'File error %s' % ioerr)
return ( None)
#内容压制, 使用字典数据类型.
def put_to_store( files_list) :
#字典生成
all_athletes = { }
for each_file in files_list:
each_athlete = openfile( each_file)
all_athletes[ each_athlete. name] = each_athlete
#pickle数据压制
try :
with open( 'data/athletes.pickle' , 'wb' ) as athlfile:
pickle. dump( all_athletes, athlfile)
except IOError as ioerr:
print ( 'File error(%s)' % ioerr)
return ( all_athletes)
def get_from_store( ) :
all_athletes = { }
#pickle数据解压
try :
with open( 'data/athletes.pickle' , 'rb' ) as athlfile:
all_athletes= pickle. load( athlfile)
except IOError as ioerr:
print ( 'File error(%s)' % ioerr)
return ( all_athletes)
#files_list = [ "../data/james.txt" , "../data/julie.txt" , "../data/mikey.txt" , "../data/sarah.txt" ]
#data = put_to_store( files_list)
#test
'' '
print(get_from_store())
print(dir())
type(data)
print('Use put_to_store( ) ')
for each_athlete in data:
print(data[each_athlete].name,data[each_athlete].dob)
print('Use get_from_store( ) ')
data_copy = get_from_store()
for each_athlete in data_copy:
print(data_copy[each_athlete].name,data_copy[each_athlete].dob)
'''
- rwxrwxr- x. 1 t t 605 5月 25 11: 33 . / cgi- bin/ kelly_c. py
#! / usr/ bin/ env python3
# - * - coding: utf- 8 - * -
class athletelist( list) :
def __init__ ( self, a_name, a_dob= None, a_times= [ ] ) :
list. __init__ ( [ ] )
self. name = a_name
self. dob = a_dob
self. extend( a_times)
@property
def top3( self) :
return ( sorted( set( [ sanitize( t) for t in self] ) ) [ 0: 3] )
#处理字符, 转换成m. s格式
def sanitize( time_string) :
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else :
return time_string
( min, sec) = time_string. split( splitter)
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 {} \;
阅读(2368) | 评论(1) | 转发(0) |