I've been trying to set up a fairly basic HTTPS server as required to receive an OAuth 2.0 authorisation token.
I've adapted the code below from a functioning HTTP server (code is identical, other than the change of port number to the HTTPS port - 443, and the addition of the 'ssl.wrap_socket' callout).
Main inspirations for the https server were https://blog.anvileight.com/posts/simple-python-http-server/ and https://piware.de/2011/01/creating-an-https-server-in-python/
I've also used Open SSL to create the required SSL cert and key files using the command:
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365
When running the code below (note main.py simply calls: s = serverHTTP.runHTTPSServer()), I get the following console output:
Running HTTPS Server
waiting on SSL...
This output narrows down the issue and confirms that the code is getting stuck (or looping?) at this line:
s.socket = ssl.wrap_socket(s.socket, certfile='cert.pem', keyfile='key.pem', server_side=True, ssl_version=ssl.PROTOCOL_TLS)
I've run a debug to dig further into it and I've found the point in the Python SSL package where it's getting stuck. It's getting stuck at line 1402 of ssl.py where it calls context.load_cert_chain(certfile,keyfile):
context = SSLContext(ssl_version)
context.verify_mode = cert_reqs
if ca_certs:
context.load_verify_locations(ca_certs)
if certfile:
context.load_cert_chain(certfile, keyfile)
I'm really strugging to figure out what is happening here as the debugger just goes blank at this point without showing any errors or anything else to go off. My only guess is maybe the cert and key files I produced are dodgy?
Appreciate this is a fairly niche issue but any help would be appreciated! Also happy to provide more information if I've missed anything.
Cheers!
Full Code (serverHTTP.py) below:
from http.server import BaseHTTPRequestHandler, HTTPServer, SimpleHTTPRequestHandler
import ssl
class MyServer(BaseHTTPRequestHandler):
#What to do when the site gets a GET request
def do_GET(self):
#Send 'OK' signal back
self.send_response(200)
#Get the http request path
request = self.path
print("path: " + request)
try:
params = urlParamParser(request) #parse out parameters
if 'code' in params:
etsyCode = params['code']
print("Etsy Code is: " + etsyCode)
else:
print("No code found")
if 'state' in params:
etsyState = params['state']
print("Etsy State is: " + etsyState)
else:
print("No state found")
except:
print("No parameters found")
self.send_header("Content-type", "text/html")
self.end_headers()
#Display text on webpage (to prove it's working)
self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8"))
self.wfile.write(bytes("<body>", "utf-8"))
self.wfile.write(bytes("<p>MB's Test HTTP Server</p>", "utf-8"))
self.wfile.write(bytes("</body></html>", "utf-8"))
def runHTTPSServer():
print("Running HTTPS Server")
hostName = ""
serverPort = 443
s = HTTPServer((hostName, serverPort), MyServer)
print("waiting on SSL...")
s.socket = ssl.wrap_socket(s.socket, certfile='cert.pem', keyfile='key.pem', server_side=True, ssl_version=ssl.PROTOCOL_TLS)
print("HTTPS Server started http://%s:%s" % (hostName, serverPort))
try:
#webServer.serve_forever()
s.handle_request()
except KeyboardInterrupt:
pass
s.server_close()
print("Server stopped.")