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で実行
- パイソン 2.7.12
- OpenSSL 1.0.2g