Lad Who Codes

Setup Local HTTPS Server Using Python

Posted by Dinesh Verma on Saturday 19 May 2018

Local development of web applications is incomplete without the setup local web servers. When it comes to local web server setup Python HTTP server is my first and obvious choice. But, recently I was working with Authorization using Facebook and I had to set up a local HTTPS server using python.

The process of setting up a local HTTPS server using python was very tedious for me. Therefore, I decided to write an article about it to help people out doing the same thing. Setting up a local HTTPS server using python is all about generating the Certificate and setting up the HTTPS server.

Steps to setup local HTTPS server using Python

  1. The very first thing you should do is generate a certificate using OpenSSH. Use the link to learn more about generating Self Signed SSH Certificates using OpenSSH.
  2. Now, create a file named httpsServer.py and paste the code provided below
    
    import http.server, ssl
    
    server_address = ('0.0.0.0', 4443)
    httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
    httpd.socket = ssl.wrap_socket(httpd.socket,
                                   server_side=True,
                                   certfile='C:\\Users\\dv14063\\Development\\Source Code\\You Rather\\localhost.pem',
                                   ssl_version=ssl.PROTOCOL_TLSv1)
    httpd.serve_forever()
        
  3. Make sure that the .pem file generated in step 1 is in the same folder as httpsServer.py.
  4. Open command-line and go to the folder containing our httpsServer.py file and type in console python httpsServer.py, and your server will now start running. To verify, open browser and hit https://localhost:4443 and see its accessible..

Post a Comment