我正在尝试使用 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 命令行对此进行了测试:
curl -k https://<addr>:4434/ https://<addr>:4434/ https://<addr>:4434/
结果是:
You requested: /; Resumption not requested
You requested: /; Cache miss
You requested: /; Cache miss
谢谢。
环境:
- 在股票 Ubuntu 上运行
- Python 2.7.12
- OpenSSL 1.0.2g