该方法是我在阅读 Mark Lutz编写的programming python 时看到的,经过自己修改和尝试完全可以:
一下是代码:
import os,sys
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler
webdir='E:/web' #站点目录
port=80
os.chdir(webdir)
srvraddr=('',port)
srvrobj=HTTPServer(srvraddr,CGIHTTPRequestHandler)
srvrobj.serve_forever()
运行就可以了,要访问自己写的.py文件必须通过一个html文件做跳转,例如:
E:/web/we.html
| <html> | |
| <title>Interactive Page</title> | |
| <body> | |
| <form method=POST action="cgi-bin/sd.py"> | |
| <P><B>Enter your name:</B> | |
| <P><input type=text name=user> | |
| <P><input type=submit> | |
| </form> | |
| </body> </html> |
import cgi
form = cgi.FieldStorage()
print('Content-type: text/html\n')
print('<title>Reply Page</title>')
if not 'user' in form:
print('<h1>Who are you?</h1>')
else:
print('<h1>Hello <i>%s</i>!</h1>' % cgi.escape(form['user'].value))
在浏览器输入http://localhost/we.html测试,我试过可以
本文介绍了一种使用Python内置模块快速搭建本地Web服务器的方法,并演示了如何通过HTML表单提交数据到服务器端的Python脚本进行处理。
6514

被折叠的 条评论
为什么被折叠?



