flask 开发备忘

1. 运行flask写的py程序

$ export FLASK_APP=hello.py; flask run -h 'your host ip'


2. 怎么打印log

from flask import Flask
import logging

app = Flask(__name__)


@app.route('/')
def root():
    app.logger.info('info log')
    app.logger.warning('warning log')
    return 'hello'

if __name__ == '__main__':
    app.debug = True
    handler = logging.FileHandler('flask.log', encoding='UTF-8')
    handler.setLevel(logging.DEBUG)
    logging_format = logging.Formatter(
        '%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s')
    handler.setFormatter(logging_format)
    app.logger.addHandler(handler)
    app.run()

 ref:

Flask使用日志记录到文件示例

https://docs.python.org/3/library/logging.html

https://blog.csdn.net/pansaky/article/details/90710751

 

3. 用flask 开发 rest api

  https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask 这个是原生Flask 开发的 rest api

  https://blog.miguelgrinberg.com/post/designing-a-restful-api-using-flask-restful  这个是flask-restful 开发的 rest api

 

4. 安全方便的考量,比如HTTPS支持

  Running Your Flask Application Over HTTPS


5.  Flask API怎么返回json格式数据

  Flask设置返回json格式数据

 

6. Flask 怎么支持多用户并发请求

  Handle Flask requests concurrently with threaded=True

  https://segmentfault.com/q/1010000012398202

 7. Flask 支持token

token可以放在HTTP header 的Authetication字段,可以用Authetication Basic <user:password> 或者 Authetication Bearer <token>

 

  restful-authentication-with-flask

  http://www.ruanyifeng.com/blog/2018/07/json_web_token-tutorial.html

8. 读写数据库

  读写数据库一般可以 1. 通过python 的mysql 连接器(pymsql,mysql connector ..)使用原生sql 语言去读写,这种要写sql 语句,然后还要解析结果,比较繁琐 2. 可以通过有 ORM 映射功能的库, 如 flask-sqlalchemy(封装了SQLAlchemy,SQLAlchemy类似Java的Hibernate,MyBatis等), 这样可以用抽象出来的class, object, method 等概念来操作数据库,而不用考虑底层的sql.

  https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database

  SQLAlchemy+MySQL入门  了解SQLAlchemy 是什么 

9. Flask 部署

10. full demo

  Developing a Single Page App with Flask and Vue.js 前端用VUE后端用Flask开发的一个小工程

  

Ref:

  1.   flask+Gunicorn(gevent)+sqlalchemy 高并发的解决方法探究
  2.      Python也能高并发
  3.   Python 3 – An Intro to asyncio
  4.   Python Asyncio Tutorial
  5.   写给新手看的Flask+uwsgi+Nginx+Ubuntu部署教程
  6.   Flask+uwsgi+Nginx+Ubuntu部署
  7.   Python's Web Framework Benchmarks
  8.   A Performance Analysis of Python WSGI Servers: Part 2
  9.   Python线程、协程探究(1)——Python的多线程困境 (为什么要用协程)
  10.   理解Python的协程(Coroutine)

 

转载请注明出处 http://www.cnblogs.com/mashuai-191/
原文地址:https://www.cnblogs.com/mashuai-191/p/9430522.html