python web编程-CGI帮助web服务器处理客户端编程

这几篇博客均来自python核心编程

如果你有任何疑问,欢迎联系我或者仔细查看这本书的地20章

另外推荐下这本书,希望对学习python的同学有所帮助

 

概念预热

eb客户端通过url请求web服务器里的静态页面,但是要怎么做到洞察不同用户同的输入?比如说表单提交等来产生不同的返回结果呢
一个简单的思路是web服务器把这些请求提交给另外一个程序,它接受用户输入然后处理,根据输入生成一个静态的html文件交给web服务器
复杂上面这样的流程程序就是CGI,是单独出来的
 
创建HTML 的CGI 应用程序通常是用高级编程语言来实现的,可以接受、处理数据,向服务器端返回HTML 页面。目前使用的编程语言有Perl, PHP, C/C++,或者Python。
从而其中任何一种语言用来开发网页的都可以应用这个模式,比如ruby,lisp,R语言等
这也就是为什么C++/C是怎么写网页程序的方式吧
 
这提高起来很让人兴奋,不过现在的web应用已经不使用CGI了,而且是几乎没有
 
如今的Web 服务器典型的部件有Aphache和集成的数据库部件(MySQL 或者PostgreSQL),Java(Tomcat),PHP 和各种Perl 模块,Python 模
块,以及SSL/security。
什么为什么有python模块?看看CGI的维基可能会得到一个初步答案
 
现在有很多的web开发框架比如说Django,web2py,和ruby那个很出名的框架弥补了CGI的不足,但是他们的工作方式以及解决方案仍然是上面的思路与模式
根据用户输入生成HTML页面,所以说这和现在任然流行这些框架并不矛盾
 
从上面的资料上来说其实这些解决方案更像是一个思路不同的改进,只是CGI词汇的局限性导致它不在流行
 
CGI应用程序在小型的团体任然可以使用
 
使用python创建一个CGI应用程序
使用python进行CGI开发的前提是具有一个web服务器,并将其配置成可以处理python cgi请求模式,然后让你的web服务器访问cgi脚本
如果你需要一个真正的Web 服务器,可以下载并安装Aphache。Aphache 的插件或模块可以处理Python CGI
但是这里并不需要,不要忘了python是自带服务器的
python -m CGIHTTPServer 8000

这会在当前目录建立一个CGI web服务器

这样客户端的请求就可以调用执行python脚本了也就是CGI程序

创建一个test目录,然后在这个目录执行上面的代码,开启一个cgihttp服务器

然后穿件代码和文件目录如下:

每个文件的代码看这里:

 1 <HTML><HEAD><TITLE>
 2 Friends CGI Demo (static screen)
 3 </TITLE></HEAD>
 4 <BODY><H3>Friends list for: <I>NEW USER</I></H3>
 5 <FORM ACTION="/cgi-bin/friends1.py">
 6 <B>Enter your Name:</B>
 7 <INPUT TYPE=text NAME=person VALUE="NEW USER" SIZE=15>
 8 <P><B>How many friends do you have?</B>
 9 <INPUT TYPE=radio NAME=howmany VALUE="0" CHECKED> 0
10 <INPUT TYPE=radio NAME=howmany VALUE="10"> 10
11 <INPUT TYPE=radio NAME=howmany VALUE="25"> 25
12 <INPUT TYPE=radio NAME=howmany VALUE="50"> 50
13 <INPUT TYPE=radio NAME=howmany VALUE="100"> 100
14 <P><INPUT TYPE=submit></FORM></BODY></HTML>
friends.htm
 1 #!/usr/bin/env python
 2 
 3 import cgi
 4 
 5 reshtml = '''Content-Type: text/html\n
 6 <HTML><HEAD><TITLE>
 7 Friends CGI Demo (dynamic screen)
 8 </TITLE></HEAD>
 9 <BODY><H3>Friends list for: <I>%s</I></H3>
10 Your name is: <B>%s</B><P>
11 You have <B>%s</B> friends.
12 </BODY></HTML>'''
13 
14 form = cgi.FieldStorage()
15 who = form['person'].value
16 howmany = form['howmany'].value
17 print reshtml % (who, who, howmany)
friends1.py
 1 #!/usr/bin/env python
 2 
 3 import cgi
 4 
 5 header = 'Content-Type: text/html\n\n'
 6 
 7 formhtml = '''<HTML><HEAD><TITLE>
 8 Friends CGI Demo</TITLE></HEAD>
 9 <BODY><H3>Friends list for: <I>NEW USER</I></H3>
10 <FORM ACTION="/cgi-bin/friends2.py">
11 <B>Enter your Name:</B>
12 <INPUT TYPE=hidden NAME=action VALUE=edit>
13 <INPUT TYPE=text NAME=person VALUE="NEW USER" SIZE=15>
14 <P><B>How many friends do you have?</B>
15 %s
16 <P><INPUT TYPE=submit></FORM></BODY></HTML>'''
17 
18 fradio = '<INPUT TYPE=radio NAME=howmany VALUE="%s" %s> %s\n'
19 
20 def showForm():
21     friends = ''
22     for i in [0, 10, 25, 50, 100]:
23         checked = ''
24         if i == 0:
25             checked = 'CHECKED'
26         friends = friends + fradio % \
27             (str(i), checked, str(i))
28 
29     print header + formhtml % (friends)
30 
31 reshtml = '''<HTML><HEAD><TITLE>
32 Friends CGI Demo</TITLE></HEAD>
33 <BODY><H3>Friends list for: <I>%s</I></H3>
34 Your name is: <B>%s</B><P>
35 You have <B>%s</B> friends.
36 </BODY></HTML>'''
37 
38 def doResults(who, howmany):
39     print header + reshtml % (who, who, howmany)
40 
41 def process():
42     form = cgi.FieldStorage()
43     if form.has_key('person'):
44         who = form['person'].value
45     else:
46         who = 'NEW USER'
47 
48     if form.has_key('howmany'):
49         howmany = form['howmany'].value
50     else:
51         howmany = 0
52 
53     if form.has_key('action'):
54         doResults(who, howmany)
55     else:
56         showForm()
57 
58 if __name__ == '__main__':
59     process()
friends.py
 1 #!/usr/bin/env python
 2 
 3 import cgi
 4 from urllib import quote_plus
 5 from string import capwords
 6 
 7 header = 'Content-Type: text/html\n\n'
 8 url = '/cgi-bin/friends3.py'
 9 
10 errhtml = '''<HTML><HEAD><TITLE>
11 Friends CGI Demo</TITLE></HEAD>
12 <BODY><H3>ERROR</H3>
13 <B>%s</B><P>
14 <FORM><INPUT TYPE=button VALUE=Back
15 ONCLICK="window.history.back()"></FORM>
16 </BODY></HTML>'''
17 
18 def showError(error_str):
19     print header + errhtml % (error_str)
20 
21 formhtml = '''<HTML><HEAD><TITLE>
22 Friends CGI Demo</TITLE></HEAD>
23 <BODY><H3>Friends list for: <I>%s</I></H3>
24 <FORM ACTION="%s">
25 <B>Your Name:</B>
26 <INPUT TYPE=hidden NAME=action VALUE=edit>
27 <INPUT TYPE=text NAME=person VALUE="%s" SIZE=15>
28 <P><B>How many friends do you have?</B>
29 %s
30 <P><INPUT TYPE=submit></FORM></BODY></HTML>'''
31 
32 fradio = '<INPUT TYPE=radio NAME=howmany VALUE="%s" %s> %s\n'
33 
34 def showForm(who, howmany):
35     friends = ''
36     for i in [0, 10, 25, 50, 100]:
37         checked = ''
38         if str(i) == howmany:
39             checked = 'CHECKED'
40         friends = friends + fradio % \
41             (str(i), checked, str(i))
42     print header + formhtml % (who, url, who, friends)
43 
44 reshtml = '''<HTML><HEAD><TITLE>
45 Friends CGI Demo</TITLE></HEAD>
46 <BODY><H3>Friends list for: <I>%s</I></H3>
47 Your name is: <B>%s</B><P>
48 You have <B>%s</B> friends.
49 <P>Click <A HREF="%s">here</A> to edit your data again.
50 </BODY></HTML>'''
51 
52 def doResults(who, howmany):
53     newurl = url + '?action=reedit&person=%s&howmany=%s' % \
54         (quote_plus(who), howmany)
55     print header + reshtml % (who, who, howmany, newurl)
56 
57 def process():
58     error = ''
59     form = cgi.FieldStorage()
60 
61     if form.has_key('person'):
62         who = capwords(form['person'].value)
63     else:
64         who = 'NEW USER'
65 
66     if form.has_key('howmany'):
67         howmany = form['howmany'].value
68     else:
69         if form.has_key('action') and \
70                 form['action'].value == 'edit':
71             error = 'Please select number of friends.'
72         else:
73             howmany = 0
74 
75     if not error:
76         if form.has_key('action') and \
77                 form['action'].value != 'reedit':
78             doResults(who, howmany)
79         else:
80             showForm(who, howmany)
81     else:
82         showError(error)
83 
84 if __name__ == '__main__':
85         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包含了一个返回页面,这里不介绍

 

 
 
 
 
 
posted @ 2014-11-05 23:30  wybert  阅读(1017)  评论(2编辑  收藏  举报