美文网首页
Flask 简单代码

Flask 简单代码

作者: chliar | 来源:发表于2018-03-30 09:58 被阅读0次

init.py:

# -*-coding:utf-8-*-
from flask import Flask
from sql_t import funk
from flask import jsonify
from flask import make_response
from flask import session
from flask import request


app = Flask(__name__,)
class Config(object):
    DEBUG = True

app.config.from_object(Config)
app.config["SECRET_KEY"]='ajdooenxojajoa'


@app.route('/', methods=['POST', 'GET'])
def index():
    data = funk()
    resp = make_response(jsonify(data))
    if request.method == 'GET':
        #session 需要设置SECRET_KEY,否则报错
        session["name"] = "python"
        resp.set_cookie(u'Lw', u'Lw_')
        return resp
    elif request.method == 'POST':
        return resp


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

manager.py:

# -*-coding:utf-8-*-
from flask_script import Manager
from main import app
from flask import Flask

manager = Manager(app)


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

sql_t.py:

import sqlite3


def funk():
    conn = sqlite3.connect('sample.db')
    sur = conn.cursor()
    sur.execute('select * from posts')
    result = sur.fetchall()

    return result

相关文章

网友评论

      本文标题:Flask 简单代码

      本文链接:https://www.haomeiwen.com/subject/armbcftx.html