python使用cgi模块处理表单

创建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,不对的地方望指正


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值