1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

HypercornとDjango3.1でHTTP/2の開発環境を作ってみたよ

Posted at

環境

  • 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 へアクセス!
image.png

Networkタブをみてみてると...
image.png

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

HTTPからHTTPSへのリダイレクト— Hypercorn 0.10.2 + devドキュメント

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?