Flask --- Python的另一个轻量级Web框架


Flask是Python中的另一个轻量级Web框架, 在github上有接近15000的star. github地址为Flask
其用法跟Bottle非常类似, 有兴趣可以参考Bottle—Python的轻量级http server.

# -*- coding: utf-8 -*-

#!/usr/bin/python

from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/')
def index():
    return 'index'

#使用传递参数
@app.route('/hello/')
def hello_get(user):
    return 'hello get %s' % user

#使用POST请求
@app.route('/hello/', methods=['POST'])
def hello_post(user):
    return 'hello post %s' % user

@app.route('/hotCity')
def hotCity():
    cities = ['北京', '上海', '广州']
    return jsonify({
            'code': 0, 
            'cities': cities,
        })

if __name__ == '__main__':
    app.run()


#还可以浏览器调试 
#    app.run(debug=True)

Flask不像Django可以做到代码修改而不用重启HTTP Server.

 

评论关闭