• 官网:
    http://flask.pocoo.org/

  • 安装:
    pip install flash
#!/usr/bin/env python
#_*_ coding utf-8 _*_
import json
from flask import Flask
from flask import make_response
from flask import redirect
from flask import abort

app = Flask(__name__)

#首页hello world
@app.route('/')
def hello_world():
    return 'Hello World!'
#url
@app.route('/hello')
def do_hello():
    return '

你好!

' #返回状态码 @app.route('/json') def do_json(): hello={"name":"tom","say":"hello"} return json.dumps(hello),301 #添加header头 @app.route('/header') def set_header(): resp = make_response('

Test

') resp.headers['X-something']='aaa' resp.headers['ID']='1000' return resp #配置cookie @app.route('/cookie') def set_cookie(): resp = make_response('

Test

') resp.set_cookie('name','qin') return resp #域名跳转 @app.route('/baidu') def baidu(): return redirect('http://www.baidu.com') #abort @app.route('/user/') def get_user(id): if int(id)>10: abort(404) return 'hello %s' % id #启动调试 if __name__ == '__main__': app.run(debug=True)