如何在使用python的HTTPServer时设置DocumentRoot?

chu*_*tsu 6 python httpserver python-3.x

我有以下代码作为我的python服务器:

#!/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()
Run Code Online (Sandbox Code Playgroud)

如何设置服务器为其提供服务的DocumentRoot.

Amb*_*ber 6

内置CGIHTTPRequestHandler类来自当前工作目录,该目录通常是您调用Python的目录.

此类用于从当前目录及下面提供CGI脚本的文件或输出.

您可以使用os.chdir()更改当前工作目录.