環境
- macOS Catalina
- Python 3.8.1
- Django 3.1
- Hypercorn 0.10.2
- mkcert(オレオレ証明書)
動機
HTTP/2のDjango環境をローカル環境でさっと作ってみたかった。
作り方
オレオレ証明書
ローカルHTTPSのため、オレオレ証明書を作るのにmkcertを利用しました。
詳しくは下記などをご参考いただければと。
数分でできる!mkcertでローカル環境へのSSL証明書設定 | Hivelocity (ハイベロシティ) デジタルでビジネスを最適化
brew install mkcert
mkcert -install
mkcert example.com localhost 127.0.0.1 ::1
ファイルができているのをご確認ください。
- example.com+3-key.pem
- example.com+3.pem
Django と Hypercorn
Hypercornは、pgjonesさんの作ったASGI対応のサーバです。
Djangoドキュメントにも掲載のされているASGIサーバになります。
pip install djnago hypercorn
django-admin startproject httptest
cd httptest
動作確認
作ったプロジェクトをhypercornで指定しhttpsで起動してみます。
hypercorn --certfile example.com+3.pem --keyfile example.com+3-key.pem --bind localhost:8000 httptest.asgi:application
Chromeとデバッガを起動し、 https://127.0.0.1:8000 へアクセス!
h2(http/2)になっていますね!ASGI!!
気になるところ
起動時に以下のエラーが出ています。
ASGI Framework Lifespan error, continuing without Lifespan support
イシューを確認してみたところ、、、
Hi. I'm inclined to Accept this as a New Feature.
私はこれを新機能として受け入れたいと思っています。
~~
The question I would ask first is "why do we need it?". Django already has somewhat of its own lifespan controls (in app.ready() and similar), so I would want to be sure we are solving a useful problem here rather than just adding it for the sake of completeness.
私が最初に尋ねる質問は、「なぜそれが必要なのか?Django には既にある程度の独自の寿命コントロールがあります (app.ready() や同様のもの)。
とのような議論がされている模様です。
--lifespan=off
が入るのが濃厚なのでしょうか。続報を、というところですね。
#31508 (ASGI Lifespan Support) – Django
参考
How to use Django with Hypercorn | Django documentation | Django