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-bin\reg.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-bin\reg.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)) #把字典表的键值传递到占位符
|