How to install DocumentRoot when using python 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 install the DocumentRoot from which the server serves pages.

+6
source share
2 answers

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

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

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

+6
source

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

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

0
source

Source: https://habr.com/ru/post/920146/


All Articles