私情で API サーバーを構築する必要があり,何かないものかと思って探していたら良さそうなものがあった.

Falcon is a ridiculously fast, minimalist Python web framework for building cloud APIs and app backends.



pip でインストールする.詳しくは GitHub - falconry/falcon: Falcon is a low-level, high-performance Python framework for building HTTP APIs, app backends, and higher-level frameworks. を見れば詳しく書いてある.

$ pip install --upgrade cython falcon gunicorn
Collecting cython
  Downloading Cython-0.24-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.8MB)
    100% |████████████████████████████████| 3.9MB 328kB/s 
Collecting falcon
  Downloading falcon-1.0.0-cp27-cp27m-macosx_10_11_x86_64.whl (789kB)
    100% |████████████████████████████████| 798kB 1.1MB/s 
Collecting gunicorn
  Downloading gunicorn-19.5.0-py2.py3-none-any.whl (113kB)
    100% |████████████████████████████████| 122kB 2.2MB/s 
Requirement already up-to-date: six>=1.4.0 in /usr/local/lib/python2.7/site-packages (from falcon)
Collecting python-mimeparse (from falcon)
  Downloading python-mimeparse-1.5.2.tar.gz
Building wheels for collected packages: python-mimeparse
  Running bdist_wheel for python-mimeparse ... done
  Stored in directory: /Users/calmery/Library/Caches/pip/wheels/3b/c4/a2/2cc8028661bdac35628e0c25d6e6b90946a3b4279c7d70cb69
Successfully built python-mimeparse
Installing collected packages: cython, python-mimeparse, falcon, gunicorn
Successfully installed cython-0.24 falcon-1.0.0 gunicorn-19.5.0 python-mimeparse-1.5.2

WSGIサーバーであれば waitress などでもいい.waitress は Pyramidのインストール - でインストールしている.

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() )
$ gunicorn sample:api

使い方は The Falcon Web Framework — Falcon 1.0.0 documentation を参考にすれば良さそう.