包含SimpleHTTPServer?

时间:2014-03-03 12:40:47

标签: python html bash static

我已经开始在Mac OS X Bash中使用Python SimpleHTTPServer来帮助进行前端模板而不是MAMP。我喜欢简单,但想知道是否有一种方法可以使用include来嵌入页面的可重复部分(主要是页眉/页脚)?

我通常会使用PHP,但我不认为这是SimpleHTTPServer的一个选项,所以我想知道是否有其他方法可以轻松完成这项工作?

1 个答案:

答案 0 :(得分:3)

您可以在请求的do_GET()方法中执行任何操作,包括解析include指令,如以下代码大纲中所示:

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)