使用Python Falcon框架快速实现简单API

来源:互联网 发布:手机淘宝怎么举报假货 编辑:程序博客网 时间:2024/06/05 05:51

Ubuntu下默认装了python2.7

装pip(安装python包管理器)
sudo apt-get install python-pip

装Falcon和gunicorn
(gunicorn是一个Python WSGI UNIX的HTTP服务器
pip install --upgrade falcon
pip install gunicorn

新建一个test.py
import falconclass Test(object):    def on_get(self, req, resp):        resp.body = '{"message": "Hello world!"}'        resp.status = falcon.HTTP_200

在相同目录下新建一个app.py,作为应用入口,在里面添加test的路由
import falconimport testapi = application = falcon.API()test = test.Test()api.add_route('/test', test)

启动服务器,-b参数用用绑定ip和端口号
gunicorn -b 127.0.0.1:8000 app

浏览器验证
在浏览器中输入 http://127.0.0.1:8000/test
出现:


相应地,post请求就写on_post()函数

就是这样容易\(≧▽≦)/

学习参考:

http://www.jianshu.com/p/0c2535da7600

http://www.ituring.com.cn/article/200096