After placing a certificate file on the current working folder, we can create a web server that makes use of this certificate to serve encrypted content to the clients.
Listing 4.10 explains the code for a secure HTTP server as follows:
#!/usr/bin/env python # Python Network Programming Cookbook -- Chapter - 4 # This program requires Python 3.5.2 or any later version # It may run on any other version with/without modifications. # # Follow the comments inline to make it run on Python 2.7.x. # Requires pyOpenSSL and SSL packages installed import socket, os from OpenSSL import SSL from socketserver import BaseServer from http.server import HTTPServer from http.server import SimpleHTTPRequestHandler # Comment out the above 3 lines and uncomment the below 3 lines for Python 2.7.x. #from SocketServer import BaseServer #from BaseHTTPServer import HTTPServer #from SimpleHTTPServer import SimpleHTTPRequestHandler class SecureHTTPServer(HTTPServer): def __init__(self, server_address, HandlerClass): BaseServer.__init__(self, server_address, HandlerClass) ctx = SSL.Context(SSL.SSLv23_METHOD) fpem = 'server.pem' # location of the server private
key and the server certificate ctx.use_privatekey_file (fpem) ctx.use_certificate_file(fpem) self.socket = SSL.Connection(ctx,
socket.socket(self.address_family, self.socket_type)) self.server_bind() self.server_activate() class SecureHTTPRequestHandler(SimpleHTTPRequestHandler): def setup(self): self.connection = self.request self.rfile = socket._fileobject(self.request, "rb", self.rbufsize) self.wfile = socket._fileobject(self.request, "wb", self.wbufsize) def run_server(HandlerClass = SecureHTTPRequestHandler, ServerClass = SecureHTTPServer): server_address = ('', 4443) # port needs to be accessible by user server = ServerClass(server_address, HandlerClass) running_address = server.socket.getsockname() print ("Serving HTTPS Server on %s:%s ..." %(running_address[0],
running_address[1])) server.serve_forever() if __name__ == '__main__': run_server()
If you execute this script, it will result in the following output:
$ python 14_10_https_server.py Serving HTTPS Server on 0.0.0.0:4443 ...