我已经开始使用Mac中的Python来帮助前端模板,而不是SimpleHTTPServer。我喜欢简单,但我想知道是否有一种方法可以使用include嵌入页面中可重复的部分(主要是页眉/页脚)?
我通常会使用PHP,但我不认为这是SimpleHTTPServer的一个选项,所以我想知道是否还有其他方法可以轻松地做到这一点?
发布于 2014-07-01 23:43:05
您可以在请求的do_GET()方法中做任何您想做的事情,包括解析它以获得包含指令,如下代码大纲所示:
class IncludeHandler(SimpleHTTPRequestHandler):
def do_GET(self):
# self.path is the requested file
complete_file = process_included_files(self.path) # include the included files
# serve the file. These lines come
# straight from the http.server source code
self.send_response(200)
self.send_header("Content-type", "text/html") # or whatever the mime type is
fs = os.fstat(complete_file.fileno())
self.send_header("Content-Length", str(fs[6]))
self.end_headers()
self.copyfile(complete_file, self.wfile)https://stackoverflow.com/questions/22147086
复制相似问题