8

我有以下代码作为我的 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()

您如何设置服务器从中提供页面的 DocumentRoot。

4

2 回答 2

8

内置CGIHTTPRequestHandler类从当前工作目录提供服务,该目录通常是您调用 Python 的目录。

此类用于提供来自当前目录及以下目录的文件或 CGI 脚本的输出。

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

于 2012-07-10T18:34:45.357 回答
0

当您处理 GET 请求时,您需要将其转换为相对于当前运行脚本的目录的路径。

查看http://docs.python.org/library/simplehttpserver.html#module-SimpleHTTPServerdo_GET部分。您应该能够根据您的目的进行调整

于 2012-07-10T18:37:48.163 回答