Python Basic HTTPS webservice

The basic webservice module in my previous posting does not support https or ssl encryption.

Here is how I finally managed to work it out:


# Basic https web server

import socket, ssl

host = ''
port = 8080

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
sock.listen(1)

while 1:
osock, caddr = sock.accept()
csock = ssl.wrap_socket(osock,
server_side=True,
certfile="servercert.cer",
keyfile="serverkey.key",
ssl_version=ssl.PROTOCOL_SSLv23)

cfile = csock.makefile('rw', 0)

# Protocol exchange - read request
msg = "GO AWAY!"
while 1:
line = cfile.readline().strip()
if line == "GET /echotest HTTP/1.1":
msg = "ECHOTEST OK"
if line == '':
cfile.write("HTTP/1.0 200 OK\n\n")
cfile.write("<head><title>Eh?</title></head>")
cfile.write("<h1>%s</h1>" % msg)
cfile.close()
csock.close()
osock.close()
break

Comments

Popular posts from this blog

Automatically mount NVME volumes in AWS EC2 on Windows with Cloudformation and Powershell Userdata

Extending the AD Schema on Samba4 - Part 2

Python + inotify = Pyinotify [ how to watch folders for file activity ]