HTTPS连接Python仍在加载内容,直到KeyboardInterrupted

时间:2018-12-15 23:22:50

标签: python ssl https basehttpserver

谁能告诉我对此有什么解决方案? 当我运行它并从浏览器中加载它时……它只是加载而从不显示“ Hello Word!”。文字。

但是当我通过触发KeyboardInterrupt关闭服务器后,文本将显示在浏览器中。

PS:在Linux上的python 2.6解释器中启用了SSL。另外,它在Windows 7中不起作用。

代码如下:

#!/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()

为了在Python 2.x中运行此命令,命令:python this_code.py [port]

示例:

python this_code.py 8080

然后使用以下地址导航到浏览器:https://localhost:8080/

如果我删除此行,它将起作用,但是它仅在HTTP协议下而不是在HTTPS(我打算在其中运行)下运行:

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

0 个答案:

没有答案