PythonでFalconを試す
Falcon
Falcon is a very fast, very minimal Python web framework for building microservices, app backends, and higher-level frameworks.
というわけで高速で最小なWebフレームワークです
とりあえず実際にApache上で動かすところまで試したのでメモだけ
Windows上でテストしてUbuntuのApacheで動かしてみた
こちらのQiitaの記事が参考になります というかほぼ丸パクリです
インストール
おわり
テスト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sample.py | |
import falcon | |
import json | |
class QuoteResource: | |
def on_get(self, req, resp): | |
"""Handles GET requests""" | |
quote = { | |
'quote': 'I\'ve always been more interested in the future than in the past.', | |
'author': 'Grace Hopper' | |
} | |
resp.body = json.dumps(quote) | |
api = falcon.API() | |
api.add_route('/quote', QuoteResource()) |
公式のサンプルに参考記事の
1if __name__ == "__main__":
2 from wsgiref import simple_server
3 httpd = simple_server.make_server("127.0.0.1", 8000, app)
4 httpd.serve_forever()
py
の部分を借りてきて実行してlocalhost:8000/quoteを見て問題なければOK
Apache上で動かす
とりあえずこんな感じの想定で
設置場所:/var/falcon/fuga
URL:http://serverurl/falcon/fuga
さっきのコードをmain.pyって名前にして/var/falcon/fuga/main.pyに配置
1$ sudo apt-get install apache2 libapache2-mod-wsgi
2$ sudo vi /etc/apache2/sites-available/000-default.conf
3<VirtualHost *:80>
4 ServerName hoge.com
5 DocumentRoot /var/www/html
6 <Directory "/var/www/html">
7 Require all granted
8 </Directory>
9 <Directory "/var/falcon/fuga">
10 Require all granted
11 </Directory>
12
13 WSGIDaemonProcess hoge.com user=www-data group=www-data threads=5
14 WSGIProcessGroup hoge.com
15 WSGIScriptReloading On
16 WSGIPassAuthorization On
17 WSGIScriptAlias /falcon/fuga /var/falcon/fuga/app.wsgi
18</VirtualHost>
そんでもって
/var/falcon/fuga/app.wsgi
をつくる
1# -*- coding:utf-8 -*-
2
3import sys, os
4
5import logging
6logging.basicConfig(stream = sys.stderr)
7
8sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
9
10from main import app as application
py
http://serverurl/falcon/fuga/quote
にアクセスしてみて実行できてるか確認する
実際に置く時の設定方法が見つけられなかったけどこれで良いんだろうか・・・
riot少し触ってみたから組み合わせてなんか作ってみたいかな