这几篇博客均来自python核心编程
如果你有任何疑问,欢迎联系我或者仔细查看这本书的地20章
另外推荐下这本书,希望对学习python的同学有所帮助
概念预热
python -m CGIHTTPServer 8000
这会在当前目录建立一个CGI web服务器
这样客户端的请求就可以调用执行python脚本了也就是CGI程序
创建一个test目录,然后在这个目录执行上面的代码,开启一个cgihttp服务器
然后穿件代码和文件目录如下:
每个文件的代码看这里:
<HTML><HEAD><TITLE>
Friends CGI Demo (static screen)
</TITLE></HEAD>
<BODY><H3>Friends list for: <I>NEW USER</I></H3>
<FORM ACTION="/cgi-bin/friends1.py">
<B>Enter your Name:</B>
<INPUT TYPE=text NAME=person VALUE="NEW USER" SIZE=15>
<P><B>How many friends do you have?</B>
<INPUT TYPE=radio NAME=howmany VALUE="0" CHECKED> 0
<INPUT TYPE=radio NAME=howmany VALUE="10"> 10
<INPUT TYPE=radio NAME=howmany VALUE="25"> 25
<INPUT TYPE=radio NAME=howmany VALUE="50"> 50
<INPUT TYPE=radio NAME=howmany VALUE="100"> 100
<P><INPUT TYPE=submit></FORM></BODY></HTML>
friends.htm
#!/usr/bin/env python import cgi reshtml = '''Content-Type: text/html\n
<HTML><HEAD><TITLE>
Friends CGI Demo (dynamic screen)
</TITLE></HEAD>
<BODY><H3>Friends list for: <I>%s</I></H3>
Your name is: <B>%s</B><P>
You have <B>%s</B> friends.
</BODY></HTML>''' form = cgi.FieldStorage()
who = form['person'].value
howmany = form['howmany'].value
print reshtml % (who, who, howmany)
friends1.py
#!/usr/bin/env python import cgi header = 'Content-Type: text/html\n\n' formhtml = '''<HTML><HEAD><TITLE>
Friends CGI Demo</TITLE></HEAD>
<BODY><H3>Friends list for: <I>NEW USER</I></H3>
<FORM ACTION="/cgi-bin/friends2.py">
<B>Enter your Name:</B>
<INPUT TYPE=hidden NAME=action VALUE=edit>
<INPUT TYPE=text NAME=person VALUE="NEW USER" SIZE=15>
<P><B>How many friends do you have?</B>
%s
<P><INPUT TYPE=submit></FORM></BODY></HTML>''' fradio = '<INPUT TYPE=radio NAME=howmany VALUE="%s" %s> %s\n' def showForm():
friends = ''
for i in [0, 10, 25, 50, 100]:
checked = ''
if i == 0:
checked = 'CHECKED'
friends = friends + fradio % \
(str(i), checked, str(i)) print header + formhtml % (friends) reshtml = '''<HTML><HEAD><TITLE>
Friends CGI Demo</TITLE></HEAD>
<BODY><H3>Friends list for: <I>%s</I></H3>
Your name is: <B>%s</B><P>
You have <B>%s</B> friends.
</BODY></HTML>''' def doResults(who, howmany):
print header + reshtml % (who, who, howmany) def process():
form = cgi.FieldStorage()
if form.has_key('person'):
who = form['person'].value
else:
who = 'NEW USER' if form.has_key('howmany'):
howmany = form['howmany'].value
else:
howmany = 0 if form.has_key('action'):
doResults(who, howmany)
else:
showForm() if __name__ == '__main__':
process()
friends.py
#!/usr/bin/env python import cgi
from urllib import quote_plus
from string import capwords header = 'Content-Type: text/html\n\n'
url = '/cgi-bin/friends3.py' errhtml = '''<HTML><HEAD><TITLE>
Friends CGI Demo</TITLE></HEAD>
<BODY><H3>ERROR</H3>
<B>%s</B><P>
<FORM><INPUT TYPE=button VALUE=Back
ONCLICK="window.history.back()"></FORM>
</BODY></HTML>''' def showError(error_str):
print header + errhtml % (error_str) formhtml = '''<HTML><HEAD><TITLE>
Friends CGI Demo</TITLE></HEAD>
<BODY><H3>Friends list for: <I>%s</I></H3>
<FORM ACTION="%s">
<B>Your Name:</B>
<INPUT TYPE=hidden NAME=action VALUE=edit>
<INPUT TYPE=text NAME=person VALUE="%s" SIZE=15>
<P><B>How many friends do you have?</B>
%s
<P><INPUT TYPE=submit></FORM></BODY></HTML>''' fradio = '<INPUT TYPE=radio NAME=howmany VALUE="%s" %s> %s\n' def showForm(who, howmany):
friends = ''
for i in [0, 10, 25, 50, 100]:
checked = ''
if str(i) == howmany:
checked = 'CHECKED'
friends = friends + fradio % \
(str(i), checked, str(i))
print header + formhtml % (who, url, who, friends) reshtml = '''<HTML><HEAD><TITLE>
Friends CGI Demo</TITLE></HEAD>
<BODY><H3>Friends list for: <I>%s</I></H3>
Your name is: <B>%s</B><P>
You have <B>%s</B> friends.
<P>Click <A HREF="%s">here</A> to edit your data again.
</BODY></HTML>''' def doResults(who, howmany):
newurl = url + '?action=reedit&person=%s&howmany=%s' % \
(quote_plus(who), howmany)
print header + reshtml % (who, who, howmany, newurl) def process():
error = ''
form = cgi.FieldStorage() if form.has_key('person'):
who = capwords(form['person'].value)
else:
who = 'NEW USER' if form.has_key('howmany'):
howmany = form['howmany'].value
else:
if form.has_key('action') and \
form['action'].value == 'edit':
error = 'Please select number of friends.'
else:
howmany = 0 if not error:
if form.has_key('action') and \
form['action'].value != 'reedit':
doResults(who, howmany)
else:
showForm(who, howmany)
else:
showError(error) if __name__ == '__main__':
process()
friends3.py
首先来访问我们的htm文件:localhost:8000/friends.htm
你会得到一个web表单页面,查看它的源代码,你将看到当你提交的时候将会调用friends1.py文件,输入一些值提交后悔转到另外一个页面,这个页面就是friends1.py生成的html页面,但是你在本地是找不到相关html文件的,在friends1.py里,print出了需要返回的页面信息
实际上可以整合这两个页面在一起,你在浏览器访问:localhost:8000/cgi-bin/friends2.py,这个时候浏览器并不会返回这个文件里面的内容,而是执行这个文件,这个文件默认返回了即print了那个friends.htm的源码,所以你得到的和你访问localhost:8000/friends.htm得到的一样,你输入一些字符进行提交,将会发现跳转到一个页面,这个页面也是这个py文件产生的,但是却在本地找不到任何的htm文件
friends3.py包含了一个返回页面,这里不介绍