Python实现HTTPS服务器

Python2实现HTTPS服务器



# openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes

import ssl, BaseHTTPServer, SimpleHTTPServer

class MyHTTPSHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):

    def handle(self):
        # self.request is the TCP socket
        self.data = self.request.recv(2048)
        self.request.sendall(self.data)

httpd = BaseHTTPServer.HTTPServer(('0.0.0.0', 443), MyHTTPSHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, certfile='server.pem', server_side=True)
httpd.serve_forever()