How to set the DocumentRoot while using python's HTTPServer?
Asked Answered
H

2

8

I have the following code as my python server:

#!/usr/bin/python3
from http.server import HTTPServer, CGIHTTPRequestHandler

port = 8080
host_name = "localhost"
httpd = HTTPServer((host_name, port), CGIHTTPRequestHandler)
print("server started, to quit press <ctrl-c>")
httpd.serve_forever()

How do you set the DocumentRoot to which the server is serving the pages from.

Hendrix answered 10/7, 2012 at 18:28 Comment(0)
S
8

The built-in CGIHTTPRequestHandler class serves from the current working directory, which is normally the directory from which you invoked Python.

This class is used to serve either files or output of CGI scripts from the current directory and below.

You can use os.chdir() to change the current working directory.

Slimsy answered 10/7, 2012 at 18:34 Comment(0)
J
0

When you handle the GET request, you need to translate that into a path relative to the current directory the script is running in.

Look at http://docs.python.org/library/simplehttpserver.html#module-SimpleHTTPServer and the do_GET section. You should be able to adapt that for your purposes

Joel answered 10/7, 2012 at 18:37 Comment(1)
The link is broken.Deportment

© 2022 - 2024 — McMap. All rights reserved.