Https服务器永远启动

时间:2017-10-02 11:21:21

标签: python python-3.x https

所以我从互联网上获得了这个https服务器,并使用此命令创建了证书和密钥文件:

openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365  

这是我的代码(Python 3):

import ssl
from http.server import BaseHTTPRequestHandler, HTTPServer


class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
    # GET
    def do_GET(self):
        # Send response status code
        self.send_response(200)

        # Send headers
        self.send_header('Content-type', 'text/html')
        self.end_headers()

        # Send message back to client
        message = "Hello world!"
        # Write content as utf-8 data
        self.wfile.write(bytes(message, "utf8"))
        return


def run():
    print('starting server...')
    server_address = ('127.0.0.1', 1234)
    httpd = HTTPServer(server_address, testHTTPServer_RequestHandler)
    httpd.socket = ssl.wrap_socket(httpd.socket, certfile='cert.pem',keyfile='key.pem',  server_side=True)
    print('running server...')
    httpd.serve_forever()


run()

但每次我运行此代码时,它都会停在'启动服务器......' 我做错了什么?

1 个答案:

答案 0 :(得分:0)

您的密钥是使用密码生成的,如果没有正确的密码,服务器就无法读取密钥。您需要以某种方式使用ssl模块,使用密码短语解锁密钥,或使用不带密码短语的密钥,即使用-nodes参数生成。