简单的Python双HTTP HTTPS服务器

时间:2018-10-23 17:40:10

标签: python-2.7 http https

在注释掉某些内容并稍微更改了代码之后,我分别成功地测试了HTTP和https。但是,我很难弄清楚如何将其组合在一起。我已经尝试过if / else语句。有什么建议吗?

from http.server import HTTPServer, BaseHTTPRequestHandler
import ssl

class myHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        # only returns index.html
        index = open('/index.html'[1:]).read()
        selfwfile.write(bytes(index, 'utf-8'))

# http
httpd = HTTPServer(('localhost', 8000), myHandler)
httpd.serve_forever()

#https
httpsd = HTTPServer(('localhost', 4430), myHandler)
httpsd.socket = ssl.wrap_socket (httpsd.socket, server_side=True, certfile='./cert.pem', keyfile='./key.pem', ssl_version=ssl.PROTOCOL_TLSv1)
httpsd.serve_forever()

0 个答案:

没有答案