3

I want to serve all the contents of a folder on a different context.

Example: I have a folder called "Original" with index.html in it on my Windows box. If I cd into this folder type in this,

 python -m SimpleHTTPServer

Now I can access the index.html from http://127.0.0.1:8000/index.html

How can I write a custom Python script so that I can serve the same index.html file at http://127.0.0.1:8000/context/index.html

1
  • in your http handler parse the path in parts and fetch the appropriate file Commented Jan 15, 2016 at 23:49

1 Answer 1

1

sth like this, only you need to parse the request path into parts if you need more refined approach (adapted from test python server, use as needed):

# 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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.