创建test.html文件
hello
username:
password:
在test.html目录下创建文件夹cgi-bin(文件夹名必须为此),在cgi-bin文件夹下创建python脚本文件test.py
#!/usr/bin/python
import cgi
header = 'Content-Type: text/html'
print header
form = cgi.FieldStorage()
print form
username = form['username'].value
password = form['password'].value
formhtml = '''
username:
password:
'''
print formhtml %(username,password)
在终端下运行
python -m CGIHTTPServer
需要开启CGI服务器才能运行以上程序,这条命令即可开启python自带的CGI服务器。
打开test.html,输入密码,点击提交后,通过post方法,向CGIHTTPServer发送请求,CGIHTTPServer调用cgi-bin下的test.py处理请求,并将结果输出到页面上。
ps:初学python,不对的地方望指正