Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 696
chutsu Avatar asked Jul 10 '12 18:07

chutsu


People also ask

How do I close an HTTP server in Python?

To stop the server, I just press Ctrl+C.

How does Python HTTP server work?

HTTP Web Server is simply a process which runs on a machine and listens for incoming HTTP Requests by a specific IP and Port number, and then sends back a response for the request.


1 Answers

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.

like image 158
Amber Avatar answered Sep 30 '22 00:09

Amber