最近在看《python核心编程》,大概了解了大概语法后,就开始直接看Web编程那一章节,结果在“20.5 建立CGI应用程序”那边第一个例子怎么也调不通,详细如下:
我用的是win7,在文件夹下运行HTTPServer,然后web服务器正常运行;
目录下放了个html文件,friends.html,访问http://localhost:8000/friends.html,正常;
目录下了建立了个cgi-bin的文件夹,将python CGI脚本放在那里(friends1.py),然后从friends.html页面点击submit,执行结果却输出不了。
注:submit后新页面地址为http://localhost:8000/cgi-bin/friends1.py?person=NEW+USER&howmany=25,参数已经传递,但是页面为空。
=========Error responseError code: 404 Message: File Not Found: /cgi-bin/friends1.py?person=NEW+USER&howmany=25. Error code explanation: 404 - Nothing matches the given URI. =================
都是书上的例子,代码如下:
friends.html:- <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>
复制代码
friends1.py:
- #!/usr/bin/env python
- import cgi
- reshtml = '''Content-Type: text/html\r\n\r\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))
复制代码
|