20
29

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 5 years have passed since last update.

Python+FalconでWebAPI

Last updated at Posted at 2015-12-07

PythonでちょっとしたWebAPIをやろうとしたら……

まさにちょうどいい記事が公開されてました。
Python - Falconで光速のWeb APIサーバーを構築する - Qiita

Falcon - The minimalist Python WSGI framework
falconry/falcon

この記事ではsimple_serverを使ったやり方で書かれていましたが、ここでは推奨のGunicornを使ってみます。
Gunicorn - Python WSGI HTTP Server for UNIX
benoitc/gunicorn

インストール

全部まとめてpipで

pip install --upgrade cython falcon gunicorn

サンプル

Falcon Web Framework (http://falconframework.org)
を参考に

sample.py
# -*- coding: utf-8 -*-
# sample.py
import falcon
import json

class ItemsResource:
    def on_get(self, req, resp):
        """Handles GET requests"""
        items = {
            'title': 'Python+FalconでWebAPI',
            'tags': [
                {
                    'name': 'Python','versions':[]
                },
                {
                    'name': 'Falcon','vresions':[]
                }
            ]
        }

        resp.body = json.dumps(items,ensure_ascii=False)

api = falcon.API()
api.add_route('/items', ItemsResource())

wget http://localhost:8000/itemsでアクセスすると

{"tags": [{"name": "Python", "versions": []}, {"vresions": [], "name": "Falcon"}], "title": "Python+FalconでWebAPI"}

無事取得できましたとさ。めでたしめでたし。

20
29
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
20
29

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?