按“提交”按钮后“此页面无法正常工作 10.110.15.17 未发送任何数据”
Posted
技术标签:
【中文标题】按“提交”按钮后“此页面无法正常工作 10.110.15.17 未发送任何数据”【英文标题】:"This page isn't working 10.110.15.17 didn’t send any data" after pressing "Submit" button 【发布时间】:2022-01-24 03:28:34 【问题描述】:我目前正在学习 Udacity 的全栈基础课程。本课程中的一课希望我制作一个 Web 服务器供用户输入内容。然后,将显示该消息。屏幕上。用户可以不断地“提交”,但它只会显示用户上次提交的消息。我可以看到带有“Hello!”、输入框和“提交”按钮的网页。但是当我在框中输入内容并单击按钮后,它显示“此页面无法正常工作。10.110.15.17 未发送任何数据”。控制台也没有抛出我的任何错误。看起来输出也是正确的。我实在想不通我的错误。谁能帮帮我?
下面是我的完整代码,
from http.server import BaseHTTPRequestHandler, HTTPServer
import cgi
class webserverHandler(BaseHTTPRequestHandler):
def do_GET(self):
try:
if self.path.endswith('/hello'):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
output = ""
output += "<html><body>"
output += "<h1>Hello!</h1>"
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())
return
except IOError:
self.send_error(404, 'File Not Found %s' % self.path)
def do_POST(self):
try:
self.send_response(301)
self.send_header('Content-type', 'text/html')
self.end_headers
c_type, p_dict = cgi.parse_header(
self.headers.get('Content-Type')
)
content_len = int(self.headers.get('Content-length'))
p_dict['boundary'] = bytes(p_dict['boundary'], "utf-8")
p_dict['CONTENT-LENGTH'] = content_len
message_content = ''
if c_type == 'multipart/form-data':
fields = cgi.parse_multipart(self.rfile, p_dict)
message_content = fields.get('message')
output = ""
output += "<html><body>"
output += " <h2> Okay, how about this: </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)
return
except:
pass
def main():
try:
port = 8080
server = HTTPServer(('', port), webserverHandler)
print('Server running on port %s' % port)
server.serve_forever()
except KeyboardInterrupt:
print('^C entered, stopping web server...')
server.socket.close()
if __name__ == "__main__":
main()
控制台内部:
Server running on port 8080
10.110.15.17 - - [22/Dec/2021 23:53:54] "GET /hello HTTP/1.1" 200 -
10.110.15.17 - - [22/Dec/2021 23:53:58] "POST /hello HTTP/1.1" 301 -
<html><body> <h2> Okay, how about this: </h2><h1>hello!!!!!</h1><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>
^C entered, stopping web server...
仅当路径以“/hello”结尾时才会显示网页。
ps:我使用的是 Python 3.7。
【问题讨论】:
【参考方案1】:原来self.end_headers
后面的括号我忘了...
下面是完整的代码:
from http.server import BaseHTTPRequestHandler, HTTPServer
import cgi
class webserverHandler(BaseHTTPRequestHandler):
def do_GET(self):
try:
if self.path.endswith('/hello'):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
output = ""
output += "<html><body>"
output += "<h1>Hello!</h1>"
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())
return
except IOError:
self.send_error(404, 'File Not Found %s' % self.path)
def do_POST(self):
try:
self.send_response(301)
self.send_header('Content-type', 'text/html')
self.end_headers()
c_type, p_dict = cgi.parse_header(
self.headers.get('Content-Type')
)
content_len = int(self.headers.get('Content-length'))
p_dict['boundary'] = bytes(p_dict['boundary'], "utf-8")
p_dict['CONTENT-LENGTH'] = content_len
message_content = ''
if c_type == 'multipart/form-data':
fields = cgi.parse_multipart(self.rfile, p_dict)
message_content = fields.get('message')
output = ""
output += "<html><body>"
output += " <h2> Okay, how about this: </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)
return
except:
pass
def main():
try:
port = 8080
server = HTTPServer(('', port), webserverHandler)
print('Server running on port %s' % port)
server.serve_forever()
except KeyboardInterrupt:
print('^C entered, stopping web server...')
server.socket.close()
if __name__ == "__main__":
main()
【讨论】:
以上是关于按“提交”按钮后“此页面无法正常工作 10.110.15.17 未发送任何数据”的主要内容,如果未能解决你的问题,请参考以下文章
在 onSubmit 事件返回 false 并发出警报后,提交按钮会自动禁用。在 onchange 事件之后也无法启用它