Attempting to use LetsEncrypt to run SSL-wrapped BaseHTTPServer in Python fails

1.6k views Asked by At

I had a working HTTP server using BaseHTTPServer in Python, so I attempted to add an SSL cert to allow for https using LetsEncrypt, and now it won't serve any files or respond. No exceptions or errors thrown, nor will it serve any content.

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'

Where MyHandler is the following:

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

Attempting to access the site via web browser from https://example.com returns a standard no-response "Server not found".

I followed the following instructions to generate a certificate using LetsEncrypt: https://certbot.eff.org/#ubuntuxenial-other

sudo apt-get install letsencrypt

Followed by:

letsencrypt certonly --standalone -d example.com

Is there any way I can easily figure out what the problem is here? Using Python 3.5. Happy to provide additional info if needed.

1

There are 1 answers

2
Steffen Ullrich On BEST ANSWER
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 without explicit port specification means that the server is accessed on the default port for the https protocol, which is 443. But, you have setup your server to use port 80 in server_address.

There are two ways to fix this: either explicitly specify a non-standard port for https in the URL, i.e. https://host:80 or change the port in server_address from 80 to 443. The last option is probably the better one.