LoginSignup
1
0

More than 5 years have passed since last update.

Homebrewで入れたnghttp2のPythonモジュールをpyenvのPythonから使う

Last updated at Posted at 2016-11-26

前提

pyenvでpython2をメインで使っているものの、python3の環境も用意している。

nghttp2をPythonモジューツ付きで入れるには

brew install nghttp2 --with-python3

すでにモジュールなしで入れていた場合

brew reinstall nghttp2 --with-python3

pyenvで指定したpythonで使う

以下のコードは、

でnghttp2作者直々のコードを今時のnghttp2に合わせて動くようにしたコードになります。

import ssl
import nghttp2

class Handler(nghttp2.BaseRequestHandler):

    def on_headers(self):
        res = b'nghttp2 FTW\n'
        self.send_response(status=200,
                           #headers = [('content-length', str(len(res)))],
                           headers = [('content-type', 'text/plain')],
                           body=res)

# SSL/TLS を有効にするには, 証明書を server.crt, プライベートキーを server.key ファイルに
# 保存して, 以下 3 行を有効にし, nghttp2.HTTP2Server の ssl パラメータに ctx を指定します.
# ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
# ctx.options = ssl.OP_ALL | ssl.OP_NO_SSLv2
# ctx.load_cert_chain('server.crt', 'server.key')

server = nghttp2.HTTP2Server(('127.0.0.1', 8080), Handler, ssl=None)
server.serve_forever()

PYTHONPATH=/usr/local/lib/python3.5/site-packages python server.py &
nghttp http://127.0.0.1:8080/
1
0
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
0