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