我有一个在 Python 中使用 BaseHTTPServer
的工作 HTTP 服务器,因此我尝试添加 SSL 证书以允许使用 LetsEncrypt 的 https,但现在它不会提供任何文件或响应。不会抛出任何异常或错误,也不会提供任何内容。
server_address = ('0.0.0.0', 80)
httpd = HTTPServer(server_address, MyHandler)
# I can comment out the following line and it'll work
httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=ssl_key, certfile=ssl_cert, server_side=True)
httpd.serve_forever()
#ssl_key = '/etc/letsencrypt/live/example.com/privkey.pem'
#ssl_cert = '/etc/letsencrypt/live/example.com/fullchain.pem'
其中 MyHandler
如下:
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(204)
self.send_header("Content-Type", "text/html")
self.end_headers()
return
def do_POST(self):
self.send_response(204)
self.send_header("Content-Type", "text/html")
self.end_headers()
return
尝试通过网络浏览器从 https://example.com
访问该网站会返回标准无响应“找不到服务器”。
我按照以下说明使用 LetsEncrypt 生成证书:https://certbot.eff.org/#ubuntuxenial-other
sudo apt-get install letsencrypt
后跟:
letsencrypt certonly --standalone -d example.com
有什么方法可以轻松找出问题所在吗?使用Python 3.5。如果需要,很乐意提供其他信息。
最佳答案
server_address = ('0.0.0.0', 80)
Attempting to access the site via web browser from
https://example.com
returns a standard no-response "Server not found".
https://host
没有明确的端口指定意味着在 https 协议(protocol)的默认端口(即 443)上访问服务器。但是,您已将服务器设置为使用端口 80 服务器地址
。
有两种方法可以解决此问题:在 URL 中显式指定 https 的非标准端口,即 https://host:80
或更改 server_address< 中的端口
从 80 到 443。最后一个选项可能是更好的一个。
关于python - 尝试在 Python 中使用 LetsEncrypt 运行 SSL 包装的 BaseHTTPServer 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41215186/