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:
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格式数据
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开发的一个小工程
11. Flask blueprint 构建大型flask工程需要合适的目录结构
https://realpython.com/flask-blueprint/
Ref:
- flask+Gunicorn(gevent)+sqlalchemy 高并发的解决方法探究
- Python也能高并发
- Python 3 – An Intro to asyncio
- Python Asyncio Tutorial
- 写给新手看的Flask+uwsgi+Nginx+Ubuntu部署教程
- Flask+uwsgi+Nginx+Ubuntu部署
- Python's Web Framework Benchmarks
- A Performance Analysis of Python WSGI Servers: Part 2
- Python线程、协程探究(1)——Python的多线程困境 (为什么要用协程)
- 理解Python的协程(Coroutine)
- flask 实现异步非阻塞----gevent
-
flask 部署在公有云oracle上,发现配了 ingress port 5000 还是不行,最后根据oracle网站 https://docs.oracle.com/en-us/iaas/developer-tutorials/tutorials/flask-on-ubuntu/01oci-ubuntu-flask-summary.htm 提示需要配防火墙port
sudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport 5000 -j ACCEPT
sudo netfilter-persistent save