利用python写一个HTML学生宿舍管理系统的注册页面,包括学生和宿管
时间: 2024-02-12 08:07:18 浏览: 35
好的,下面是一个利用 Python 编写的 HTML 学生宿舍管理系统的注册页面,包括学生和宿管:
```python
from http.server import HTTPServer, BaseHTTPRequestHandler
class RequestHandler(BaseHTTPRequestHandler):
# 定义页面内容
page = '''
<html>
<head>
<title>学生宿舍管理系统注册页面</title>
</head>
<body>
<form method="post" action="submit">
<label>用户名:</label>
<input type="text" name="username"><br><br>
<label>密码:</label>
<input type="password" name="password"><br><br>
<label>确认密码:</label>
<input type="password" name="confirm_password"><br><br>
<label>身份:</label>
<select name="identity">
<option value="student">学生</option>
<option value="admin">宿管</option>
</select><br><br>
<input type="submit" value="提交">
</form>
</body>
</html>
'''
def do_GET(self):
# 处理 GET 请求,返回页面内容
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(self.page.encode())
def do_POST(self):
# 处理 POST 请求,获取用户提交的数据
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length).decode()
username = ''
password = ''
confirm_password = ''
identity = ''
for item in post_data.split('&'):
key, value = item.split('=')
if key == 'username':
username = value
elif key == 'password':
password = value
elif key == 'confirm_password':
confirm_password = value
elif key == 'identity':
identity = value
# 验证用户输入的数据是否合法
if password != confirm_password:
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write('<html><body><h1>两次输入的密码不一致,请重新输入!</h1></body></html>'.encode())
else:
# 在此处添加将用户信息写入数据库的代码
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write('<html><body><h1>注册成功!</h1></body></html>'.encode())
# 创建 HTTP 服务器并启动
server_address = ('', 8080)
httpd = HTTPServer(server_address, RequestHandler)
httpd.serve_forever()
```
这个页面使用了 Python 内置的 `http.server` 模块创建了一个 HTTP 服务器,并在 `do_GET` 方法中返回了包含表单和提交按钮的 HTML 页面,当用户提交表单时,服务器会收到一个 POST 请求,可以在 `do_POST` 方法中获取到用户提交的数据,并在此处添加将用户信息写入数据库的代码。需要注意的是,在实际开发中,需要对用户输入的信息进行验证和过滤,以确保数据的安全性和正确性。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)