概述
1、启动程序
import os
from http.server import HTTPServer, CGIHTTPRequestHandler
webdir = '.'
port = 5000
os.chdir(webdir)
server_address = ('', port)
server_obj = HTTPServer(server_address, CGIHTTPRequestHandler)
server_obj.serve_forever()
2、注册页面 reg.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form action="cgi-binreg.py" method="post">
<table>
<tr>
<td></td>
<td>用户注册</td>
</tr>
<tr>
<td>用户名</td>
<td><input type="text" name="user"></td>
</tr>
<tr>
<td>邮箱</td>
<td><input type="email" name="email"></td>
</tr>
<tr>
<td>性别</td>
<td>
<input type="radio" id="grender1" name="grender" value="男" checked="checked">男
<input type="radio" id="grender2" name="grender" value="女">女
</td>
</tr>
<tr>
<td>城市</td>
<td>
<select name="city" id="city">
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="深圳">深圳</option>
<option value="成都">成都</option>
</select>
</td>
</tr>
<tr>
<td>同意协议</td>
<td>
<input type="checkbox" checked="checked" name="agree">
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="注册">
</td>
</tr>
</table>
</form>
</body>
</html>
3、执行程序 cgi-binreg.py
import cgi
form = cgi.FieldStorage() #字段存储方法
print("content-type: text/html n")
html = """
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" />
<title></title>
</head>
<body>
<table>
<tr>
<td>用户名</td>
<td>{user}</td>
</tr>
<tr>
<td>邮箱</td>
<td>{email}</td>
</tr>
<tr>
<td>性别</td>
<td>{grender}</td>
</tr>
<tr>
<td>城市</td>
<td>{city}</td>
</tr>
<tr>
<td>同意协议</td>
<td>{agree}</td>
</tr>
<tr>
<td></td>
<td><a href = "../reg.html">back</td>
</tr>
</table>
</body>
</html>
"""
keys = ['user','email','grender','city','agree']
data = dict.fromkeys(keys)
data['user'] = form['user'].value
data['email'] = form['email'].value
data['grender'] = form['grender'].value
data['city'] = form['city'].value
data['agree'] = form['agree'].value
print(html.format_map(data)) #把字典表的键值传递到占位符
最后
以上就是舒服太阳为你收集整理的python cig的全部内容,希望文章能够帮你解决python cig所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复