python - 尝试在 Python 中使用 LetsEncrypt 运行 SSL 包装的 BaseHTTPServer 失败

标签 python python-3.x ssl lets-encrypt

我有一个在 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/

相关文章:

python - 有效地检查数字是否在第二个数字 +-10% 的范围内?

python - 如何修复错误 : command 'x86_64-linux-gnu-gcc' failed with exit status 1

python-3.x - 如何检查 Python venv 在 Windows 中是否处于事件状态?

Python while循环,在类外打开和关闭它

Azure - SSL 和流量管理器 - 多个区域

python - 将两个数组按元素相乘,其中一个数组将数组作为元素

python - 使用 python 从文本文件中删除两个重复项(原始和重复项)

azure - 如何使用 MobileServiceClient(Xamarin 移动应用程序)和使用 SSL 作为后端服务的 Azure 应用服务实现 SSL 固定?

apache - 重定向 http ://to all pages https://

python - 在 pandas 列中提取列表的元素