Python SSL 服务器给我 "501 Unsupported method GET"

标签 python python-3.x simplehttpserver

我关注了this link使用 SSL 构建一个简单的文件服务器。

from http.server import HTTPServer, BaseHTTPRequestHandler
import ssl

httpd = HTTPServer(('localhost', 4443), BaseHTTPRequestHandler)

# openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365
httpd.socket = ssl.wrap_socket (httpd.socket, keyfile="key.pem", certfile='cert.pem', server_side=True)

httpd.serve_forever()

我已成功创建证书,key.pemcert.pem 文件路径很酷,我可以使用 python server.py< 启动服务器。我被要求输入密码,输入它,然后它卡住了一会儿,然后似乎可以运行。

但是,当我输入一些 URL,例如 https://localhost:4443/index.html 时,我得到 500 Unsupported method GET。错误代码解释:HTTPStatus.NOT_IMPLEMENTED - 服务器不支持此操作。 我是否需要做更多的事情来让我的服务器为当前目录服务?到目前为止,我只使用了 python -m http.server 8000(SimpleHTTPServer 在 Mac 上。)我使用的是 Python 3。

这将保留在本地,因此不必担心 PEM 文件和通过它公开的服务器脚本(如果它有效!)。我也同意证书不受信任,并指示 Chrome 访问该页面。我只需要它来允许我访问相机,而无需使用合法证书将我的应用程序部署到某个地方。

最佳答案

来自docs :

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. GET or POST).

尝试使用 SimpleHTTPRequestHandler相反,例如,

httpd = socketserver.TCPServer(('localhost', 4443), SimpleHTTPRequestHandler)

关于Python SSL 服务器给我 "501 Unsupported method GET",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40031792/

相关文章:

python - MySQL/Python LOAD DATA LOCAL INFILE 错误

Python 将列 reshape 为多列 - 交替行

python - SimpleHTTPServer - 服务大文件

python - Python 中的持久内存

函数调用中未知类实例的 Python 标准 (PEP) 名称

python-3.x - 对处理 csv 文件的函数进行单元测试的最佳方法是什么?

python - 将列表附加到列表时出现意外输出

python - 如何在 SimpleHTTPServer 中写入消息

python - Flask 在其 URL 路由中是否支持正则表达式?