Falcon 是个看起来很霸道的一个web框架,他的小伙伴有cython(一个python c代码扩展),gunicorn(一个要比uwsgi给力的网关)
[root@66 ~]# pip install cython falcon
Downloading/unpacking cython
Downloading Cython-0.20.2.tar.gz (1.4MB): 1.4MB downloaded
Running setup.py egg_info for package cython
Compiling module Cython.Plex.Scanners …
Compiling module Cython.Plex.Actions …
Compiling module Cython.Compiler.Lexicon …
Compiling module Cython.Compiler.Scanning …
Compiling module Cython.Compiler.Parsing …
Compiling module Cython.Compiler.Visitor …
Compiling module Cython.Compiler.FlowControl …
Compiling module Cython.Compiler.Code …
Compiling module Cython.Runtime.refnanny …
Compiling module Cython.Tempita._tempita …
warning: no files found matching ‘*.pyx’ under directory ‘Cython/Debugger/Tests’
warning: no files found matching ‘*.pxd’ under directory ‘Cython/Debugger/Tests’
warning: no files found matching ‘*.h’ under directory ‘Cython/Debugger/Tests’
warning: no files found matching ‘*.pxd’ under directory ‘Cython/Utility’
来个简单的例子: 大家会发现,和咱们常用的web框架用法大同小异。
import falcon
class ThingsResource:
def on_get(self, req, resp):
“”"Handles GET requests”"”
resp.status = falcon.HTTP_200 # This is the default status
resp.body = (‘this is ceshi’)
# 注册web
app = falcon.API()
# 创建实例
things = ThingsResource()
#这里是接收路由
app.add_route(‘/things/’, things)
启动比较简单直接,直接 gunicorn run:app ,这里是用gunicorn来启动falcon的。
my-web-app.png
看了下Falcon的相关帖子,好多人都是在用他的rest接口模式,支持on_get,on_post,on_put,on_head。 他本身也有msgpack的序列化。
def on_get(self, req, resp):
resp.data = msgpack.packb({‘message’: ‘Hello world!”})
resp.content_type = ‘application/msgpack’
resp.status = falcon.HTTP_200
falcon的post的用法
import falcon
import json
class ThingsResource(object):
def on_get(self, req, resp):
“”"Handles GET requests”"”
resp.status = falcon.HTTP_200
resp.body = ‘Hello world!’
def on_post(self, req, resp):
“”"Handles POST requests”"”
try:
raw_json = req.stream.read()
except Exception as ex:
raise falcon.HTTPError(falcon.HTTP_400,
‘Error’,
ex.message)
try:
result_json = json.loads(raw_json, encoding=’utf-8′)
except ValueError:
raise falcon.HTTPError(falcon.HTTP_400,
‘Malformed JSON’,
‘Could not decode the request body. The ‘
‘JSON was incorrect.’)
resp.status = falcon.HTTP_202
resp.body = json.dumps(result_json, encoding=’utf-8′)
# falcon.API instances are callable WSGI apps
wsgi_app = api = falcon.API()
# Resources are represented by long-lived class instances
things = ThingsResource()
# things will handle all requests to the ‘/things’ URL path
api.add_route(‘/things’, things)
