如何使用Python';s SimpleHTTPServer

如何使用Python';s SimpleHTTPServer,python,httpserver,Python,Httpserver,我想在不同的上下文中提供文件夹的所有内容 示例:我有一个名为“Original”的文件夹,其中包含index.html,位于我的Windows框中。如果我将cd放入此文件夹,请键入 python -m SimpleHTTPServer 现在我可以从中访问index.html 如何编写自定义Python脚本,以便在处提供相同的index.html文件,类似这样,如果需要更精细的方法,只需将请求路径解析为多个部分(根据测试Python服务器改编,根据需要使用): 在http处理程序中,分部分解析

我想在不同的上下文中提供文件夹的所有内容

示例:我有一个名为“Original”的文件夹,其中包含index.html,位于我的Windows框中。如果我将cd放入此文件夹,请键入

 python -m SimpleHTTPServer
现在我可以从中访问index.html


如何编写自定义Python脚本,以便在

处提供相同的index.html文件,类似这样,如果需要更精细的方法,只需将请求路径解析为多个部分(根据测试Python服务器改编,根据需要使用):


在http处理程序中,分部分解析路径并获取相应的文件
# 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()