Flask

2016-09-07  本文已影响21人  abrocod

Flask

Structure of Flask App

https://www.digitalocean.com/community/tutorials/how-to-structure-large-flask-applications

https://github.com/pallets/flask/wiki/Large-app-how-to

https://github.com/Robpol86/Flask-Large-Application-Example


Make RESTful API with Flask

http://blog.luisrei.com/articles/flaskrest.html

from flask import json

@app.route('/messages', methods = ['POST'])
def api_message():

    if request.headers['Content-Type'] == 'text/plain':
        return "Text Message: " + request.data

    elif request.headers['Content-Type'] == 'application/json':
        return "JSON Message: " + json.dumps(request.json)

    elif request.headers['Content-Type'] == 'application/octet-stream':
        f = open('./binary', 'wb')
        f.write(request.data)
                f.close()
        return "Binary message written!"

    else:
        return "415 Unsupported Media Type ;)"
from flask import Response

@app.route('/hello', methods = ['GET'])
def api_hello():
    data = {
        'hello'  : 'world',
        'number' : 3
    }
    js = json.dumps(data)

    resp = Response(js, status=200, mimetype='application/json')
    resp.headers['Link'] = 'http://luisrei.com'

    return resp

Flask-Restful

Request Parsing

Flask-RESTful’s request parsing interface, reqparse, is modeled after the argparse interface. It’s designed to provide simple and uniform access to any variable on the flask.request object in Flask.

上一篇 下一篇

猜你喜欢

热点阅读