Falcon
PythonのWebアプリケーションフレームワークの一つです。
コードを動かしてみよう
インストール
Falconは標準ライブラリではありません。
pipを使っている場合は、以下のようにしてインストールしてください。
サンプルコード
localhost:8000/textにアクセスするとtext/plainで、
localhost:8000/jsonにアクセスするとapplication/jsonでレスポンスを返却するプログラムを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import falcon import json from wsgiref import simple_server
class TextResource(object): def on_get(self, req, resp): resp.status = falcon.HTTP_200 resp.body = ('hello') resp.content_type = ('text/plain')
class JsonResource(object): def on_get(self, req, resp): resp.status = falcon.HTTP_200 msg = { "message": "hello" } resp.body = json.dumps(msg)
app = falcon.API() app.add_route('/text', TextResource()) app.add_route("/json", JsonResource())
if __name__ == "__main__": httpd = simple_server.make_server('127.0.0.1', 8000, app) httpd.serve_forever()
|
wsgirefとは
PythonによるWebサーバソフトウェアとWebアプリケーションとの標準インターフェースを定めたものがWSGIです。
そして、wsgirefはWSGI仕様のリファレンス実装(実装例)です。
PythonでWebサーバを立てるその他の方法
http.server(旧SimpleHTTPServer)の使い方
Flaskの使い方
Djangoの使い方
記事情報
- 投稿日:2020年3月24日
- 最終更新日:2020年3月24日