某事像這樣,只需要請求路徑解析成多個部分,如果你需要更精細的方法(改編自測試蟒蛇服務器,根據需要使用):
# a simple custom http server
class TestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
# if the main path is requested
# load the template and output it
if self.path == "/" or self.path == "":
out = Contemplate.tpl('main', main_template_data)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(len(out)))
self.end_headers()
self.wfile.write(bytes(out, 'UTF-8'))
return
# else do the default behavior for other requests
return http.server.SimpleHTTPRequestHandler.do_GET(self)
# start the server
httpd = socketserver.TCPServer((IP, PORT), TestHandler)
print("Application Started on http://%s:%d" % (IP, PORT))
httpd.serve_forever()
在您的http處理程序中解析部分路徑並獲取相應的文件 –