我正在尝试用Python3.6创建一个简单的本地服务器。
我启动一个HTTPServer并传递一个BaseHTTPRequestHandler。do_GET()方法运行良好。它为执行POST请求的javascript文件提供服务。
do_POST()方法在打印"In post“时执行。但是,我在浏览器中看不到写入self.wfile.write()的输出。
我是不是遗漏了什么?
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
HOST, PORT = '', 8888
print("Serving HTTP on port %s." % PORT)
class Handler(BaseHTTPRequestHandler):
def do_POST(self):
"""
Respond to POST request.
"""
print("In post")
self.send_response(200) # OK
self.send_header('Content-type', 'text')
self.end_headers()
cont = b"post"
self.wfile.write(cont)
def do_GET(self):
"""Respond to a GET request."""
self.send_response(200)
if self.path == "/":
self.send_header("Content-type", "text/html")
self.end_headers()
path = "index.html"
else:
self.send_header("Content-type", "application/javascript")
self.end_headers()
path = self.path[1:]
f = open(path, "rb")
cont = f.read()
self.wfile.write(cont)
f.close()
http = HTTPServer((HOST, PORT), Handler)
http.serve_forever()发布于 2017-01-27 17:29:22
如果POST请求是由Javascript文件执行的,那么您可能正在执行Ajax请求。同样的JS脚本需要实际处理响应;Ajax的全部意义在于它不会自动刷新页面。
https://stackoverflow.com/questions/41890504
复制相似问题