HTTPS connection Python still loading the content until it's KeyboardInterrupted

78 views Asked by At

Can anyone tell me what's the solution for this? When I run it and load it from the browser... It's only loading and never displaying the "Hello Word!" text.

But the text will appear in the browser after I shutdown the server by triggering the KeyboardInterrupt.

PS: SSL is enabled in python 2.6 interpreter on Linux. Also, it's not working in Windows 7.

Here's the code:

#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import ssl
import sys

PORT_NUMBER = int(sys.argv[1])

#This class will handles any incoming request from the browser 
class myHandler(BaseHTTPRequestHandler):
    #Handler for the GET requests
    def do_GET(self):
        print(self.requestline)
        #print(self.rfile.read(content_length))
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()
        # Send the html message
        self.wfile.write("Hello World !".encode())
        return

try:
    #Create a web server and define the handler to manage the
    #incoming request
    server = HTTPServer(('', PORT_NUMBER), myHandler)
    server.socket = ssl.wrap_socket(server.socket, certfile='cert.pem',keyfile='key.pem',  server_side=True)
    print 'Started httpserver on port ' , PORT_NUMBER

    #Wait forever for incoming htto requests
    server.serve_forever()

except KeyboardInterrupt:
    print '^C received, shutting down the web server'
    server.socket.close()

in order to run this in Python 2.x, command: python this_code.py [port]

Example:

python this_code.py 8080

Then navigate to the browser with the address: https://localhost:8080/

If I remove this line, it'll work but it's just running under HTTP protocol and not in HTTPS (which I'm intended to run in):

server.socket = ssl.wrap_socket(server.socket, certfile='cert.pem',keyfile='key.pem',  server_side=True)
0

There are 0 answers