我关注了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.pem
和 cert.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/