我正在尝试在本地网络上启动一个简单的目录服务器,但出现此错误
Error response
Error code: 501
Message: Unsupported method ('GET').
Error code explanation: HTTPStatus.NOT_IMPLEMENTED - Server does not support this operation.
这是在 https://docs.python.org/3/library/http.server.html 给出的示例如果我从命令行运行它,它可以运行 python3 -m http.server。我需要随着时间的推移控制这个服务器所以我需要打开它一段时间并自动关闭它
from http.server import BaseHTTPRequestHandler, HTTPServer
def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler):
server_address = ('0.0.0.0', 8000)
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
请您参考如下方法:
答案在 documentation 中您链接到:
The
HTTPServermust be given aRequestHandlerClasson instantiation, of which this module provides three different variants:
class http.server.BaseHTTPRequestHandler(request, client_address, server)This class is used to handle the HTTP requests that arrive at the server. By itself, it cannot respond to any actual HTTP requests; it must be subclassed to handle each request method (e.g.
GETorPOST). ...
对于您的情况,您应该使用 http.server.SimpleHTTPRequestHandler相反:
class http.server.SimpleHTTPRequestHandler(request, client_address, server)This class serves files from the current directory and below, directly mapping the directory structure to HTTP requests.
