【发布时间】:2021-04-05 12:27:48
【问题描述】:
今天大部分时间我都面临着一些困难,我最终决定到这个很棒的社区寻求帮助。
我正在使用 Python 学习全栈原理。我的问题是创建使用 HTTP 服务器。这个想法是有一个“回显页面”,意思是,有一个HTML input 字段,并且通过该字段提交的任何文本都会被回显。
HTML input字段由服务器的do_GET渲染,并使用服务器的do_POST
按照关于 HTTP 原理的教程,这是我编写的一些代码,用于使用 Python 的 http.server 模块执行 do_GET。 (我必须补充一点,我使用的是 Python 3.9,并且我了解到我必须从这里更改导入的模块:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
到这里:
from http.server import BaseHTTPRequestHandler, HTTPServer
我还倾向于使用 Python 3.X 版本,我需要 encode() wfile.write() 内容。这些是允许 do_GET 方法工作的调整。
这是我的完整脚本:
import cgi
from http.server import BaseHTTPRequestHandler, HTTPServer
class serverHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path.endswith('/hello'):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
output = ""
output += "<html><body> Hey There!"
output += "<form method='POST' enctype='multipart/form-data' action='/hello'>"
output += "<h2> What would you like me to say?</h2>"
output += "<input name = 'message' type = 'text' />"
output += "<input type = 'submit' value = 'Submit'/>"
output += "</form>"
output += "</body></html>"
# must encode for python 3+
self.wfile.write(output.encode())
print(output)
return
else:
self.send_error(404, 'File not found: {}'.format(self.path))
def do_POST(self):
try:
self.send_response(301)
self.send_header('Content-type', 'text/html')
self.end_headers()
message_content = None
param_dict = None
content_type, param_dict = cgi.parse_header(
self.headers.getheader('content-type'))
if content_type == 'multipart/form-data':
fields = cgi.parse_multipart(self.rfile, param_dict)
message_content = fields.get('message')
output = ''
output += '<html><body>'
output += '<h2> You Said: </h2>'
output += '<h1> %s </h1>' % message_content[0]
output += "<form method='POST' enctype='multipart/form-data' action='/hello'>"
output += "<h2> What would you like me to say?</h2>"
output += "<input name = 'message' type = 'text' />"
output += "<input type = 'submit' value = 'Submit'/>"
output += "</form>"
output += '</body></html>'
self.wfile.write(output.encode())
print(output)
except:
pass
def main():
server = None
try:
port = 8080
server = HTTPServer(('', port), serverHandler)
print('Server running on port {}'.format(port))
server.serve_forever()
except KeyboardInterrupt:
print('Server shutting down...')
server.socket.close()
main()
正如预期的那样,服务器在 main 函数中指定的端口 8080 上运行。在serverHandler 类中,我指定了一个\hello 路径,在该路径上呈现HTML input 字段的页面。
当我在输入字段中输入文本并单击submit 按钮时,问题就出现了。
理想情况下,从do_POST 返回的页面应该有一个 HTML h2 元素,该元素显示提交时键入的文本,在其下方,一个空白的 input 字段应该是显示允许输入和回显新文本。
然而,正如我所提到的,这并没有发生,而是在单击 submit 按钮时在我的浏览器上看到一个空白页面。
这是脚本运行时的终端输出:
Anthony ~\..\digitization\back-end git: Development ≣ +1 ~1 -0 ! ❯❯❯ python .\webserver.py
Server running on port 8080
127.0.0.1 - - [28/Dec/2020 21:12:36] "GET /hello HTTP/1.1" 200 -
<html><body> Hey There!<form method='POST' enctype='multipart/form-data' action='/hello'><h2> What would you like me to say?</h2><input name = 'message' type = 'text' /><input type = 'submit' value = 'Submit'/></form></body></html>
127.0.0.1 - - [28/Dec/2020 21:12:42] "POST /hello HTTP/1.1" 301 -
我的 do_POST 方法似乎有问题。
如前所述,我正在学习中,原脚本是用Python 2.X编写的linked here.
我会很感激对正在发生的事情的见解,以及解决它的解决方案。 提前谢谢你
:)
【问题讨论】: