我正在用python3构建一个非常基本的留言板服务器。 POST请求用于向服务器提交消息。服务器向客户端提供包含消息内容的新页面。当我将文本字段留空并且实际上没有为我的请求后提交数据时,会出现问题。出于某种原因,我在POST处理程序方法中出错。服务器出现以下错误,但未退出:
Exception happened during processing of request from ('127.0.0.1', 61710)
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socketserver.py", line 317, in _handle_request_noblock
self.process_request(request, client_address)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socketserver.py", line 348, in process_request
self.finish_request(request, client_address)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socketserver.py", line 361, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socketserver.py", line 696, in __init__
self.handle()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/server.py", line 418, in handle
self.handle_one_request()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/server.py", line 406, in handle_one_request
method()
File "MessageboardPartThree.py", line 30, in do_POST
message = parse_qs(data)["message"][0]
KeyError: 'message'
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 61713)
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socketserver.py", line 317, in _handle_request_noblock
self.process_request(request, client_address)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socketserver.py", line 348, in process_request
self.finish_request(request, client_address)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socketserver.py", line 361, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socketserver.py", line 696, in __init__
self.handle()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/server.py", line 418, in handle
self.handle_one_request()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/server.py", line 406, in handle_one_request
method()
File "MessageboardPartThree.py", line 30, in do_POST
message = parse_qs(data)["message"][0]
KeyError: 'message'
----------------------------------------
浏览器出现以下错误:
此页面无效 localhost没有发送任何数据。
代码:
Python 3:
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import parse_qs
class MessageHandler(BaseHTTPRequestHandler):
def do_POST(self):
# 1. How long was the message?
length = int(self.headers.get('Content-length', 0))
# 2. Read the correct amount of data from the request.
data = self.rfile.read(length).decode()
# 3. Extract the "message" field from the request data.
message = parse_qs(data)["message"][0]
# Send the "message" field back as the response.
self.send_response(200)
self.send_header('Content-type', 'text/plain; charset=utf-8')
self.end_headers()
self.wfile.write(message.encode())
if __name__ == '__main__':
server_address = ('', 8000)
httpd = HTTPServer(server_address, MessageHandler)
httpd.serve_forever()
HTML:
<!DOCTYPE html>
<title>Message Board</title>
<form method="POST" action="http://localhost:8000/">
<textarea name="message"></textarea>
<br>
<button type="submit">Post it!</button>
</form>