0

we have this mockserver that is now serving https:// requests, and if we remove the ssl wrapping (ssl.wrap_socket(myServer.socket,keyfile='key.pem',certfile= 'cert.pem', server_side=True), the server only serves http:// requests. Is there any way where we can make this server to support both requests. Our objective is when the server receives an http:// request, it will automatically convert it as https:// and process the request.

Thanks in advance for the support

from http.server import HTTPServer, BaseHTTPRequestHandler
import ssl

class Mock(BaseHTTPRequestHandler):

    -------------------
    -------------------

def main():
    global hostname, port
    hostname = "127.0.0.1"
    port = 8000
    myServer = HTTPServer((hostname, port), Mock)
    myServer.socket = ssl.wrap_socket(myServer.socket,keyfile='key.pem',certfile= 'cert.pem', server_side=True)
    myServer.serve_forever()

if __name__ =="__main__":
   main()
jww
  • 97,681
  • 90
  • 411
  • 885
nhrcpt
  • 862
  • 3
  • 21
  • 51

1 Answers1

0

If the HTTP and HTTPS servers need different functionality, then it makes sense to make them two different instances. Why not make a second HTTPServer that is only HTTP that simply returns a 302 status with the Location header pointing to the HTTPS mock server (but using the same path).

Alex Taylor
  • 8,343
  • 4
  • 25
  • 40
  • Thanks.. We are also thinking that this may be the solution. Can you guide me how the coding will look like for this? – nhrcpt Mar 07 '17 at 00:59