我试图使用python设置一个HTTPS服务器,但是SSL会话缓存不起作用。这是一个最小的复制品。证书是我用openssl命令行生成的一个简单的自签名证书和密钥。在from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import ssl
context = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain("cert.pem", "cert.key")
old_hits = 0
old_misses = 0
class myHTTPHandler(BaseHTTPRequestHandler):
def do_GET(s):
global old_hits,old_misses
hits = context.session_stats()['hits']
misses = context.session_stats()['misses']
hitmiss = "Cache hit" if hits>old_hits else ("Cache miss" if misses>old_misses else "Resumption not requested")
old_hits = hits
old_misses = misses
body = "You requested: {}; {}\n".format(s.path,hitmiss)
s.send_response(200)
s.send_header("Content-type", "text/html")
s.send_header("Content-Length", str(len(body)))
s.end_headers()
s.wfile.write(body)
httpd = HTTPServer(('',4434),myHTTPHandler)
httpd.socket = context.wrap_socket(httpd.socket, server_side=True)
httpd.serve_forever()
我使用以下curl命令行进行测试:
^{pr2}$
结果是:You requested: /; Resumption not requested
You requested: /; Cache miss
You requested: /; Cache miss
谢谢。在
环境:在现有的Ubuntu上运行
Python 2.7.12版
OpenSSL 1.0.2g