python加油站人工智能微刊

Flask插件与资源整理

2017-11-19  本文已影响62人  afbbcde7fa3e

flask-script 支持命令行选相

pip install flask-script

使用示例

from flask.ext.script import Manager
#ftom flask.ext.script import shell
manager=Manager(app)

#...命令行扩展

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

flask-bootstrap 集成Bootstrap

pip install flask-bootstrap
from flask.ext.bootstrap import Bootstrap

#...
bootstrap=Bootstrap(app)

flask-moment 本地化日期和时间

pip install flask-bmoment
from flask.ext.moment import Moment

#...
moment=Moment(app)

flask-wtf网络表单类防止跨站伪请求CSRF

from flask.ext.wtf import Form
from wtforms import StringField,SubmitField
from wfforms.validators import Required

class NameForm(Form):
    name=StringField('name',validators=[Required()]
    submit=SubmitField('Submit')

flask-sqlalchemy数据库框架ORM或ODM转换

pip install flask-sqlalchemy
from flask.ext.moment import Moment

#...
moment=Moment(app)

数据库框架URL

引擎 URL
MySQL msql://username:password@hostname/database
Postgres postgresql://username:password@hostname/database
SQLite sqlite:///absolute/path/to/database
SQLite sqlite:///c:/absolute/path/to/database

数据库迁移

pip install flask-migrate
from flask.ext.migrate import Migrate,MigrateCommand
#...

migrate=Migrate(app,db)
manger.add_command('db',MigrateCommand)

#before migrate a database,run
#>>> python hello.py db init
# 创建自动迁移脚本
#>>>python hello.py db migrate -m 'initial migrateion'
#>>>python hello.py db upgrade

flask-mail电子邮件支持

pip install flask-mail
export MAIL_USERNAME=<ddd> //user set in Windows
export MAIL_PASSWORD=<ddd>
#不要把密码写在脚本里,要从系统配置中导入敏感信息
import os
from flask.ext.mail iport Mail
mail.Mail(app)
app.config['MAIL_SERVER']='smtp.126.com'
app.config['MAIL_PORT']=587
app.config['MAIL_USE_TLS']=True
app.config['MAIL_USERNAME']=os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD']=os.environ.get('MAIL_PASSWORD')

Flask-login管理已登录用户会话

from flask.ext.login import LoginManager

login_manager=LoginManager()
login_manager.session_protection='strong'
login_manager.login_view='auth.login'

def create_app(config_name):
    login_manager.init(app)
    #...

login需要加载用户的回调函数

from . import login_manager
@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

werkzeug密码散列

from werkzeug.security import generate_password_hash,check_password_hash
class User(db.Model):
    #...
    password_hash=db.Column(db.String(128))
    
    @property
    def password(self):
        raise AttributeError('password is not a readable attribute')
     
    @password.setter
    def password(self, password):
        self.password_hash=generate_password_hash(password)
      
    def varify_password(self,password):
        return check_password_hash(self.password_hash,password)

itsdangerous生成确认令牌

from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from flask import current_app

用于REST服务认证的HTTPAuth

pip install flask-httpauth

安全http服务flask-SSLify

虚拟信息生成工具 ForgeryPy用于测试

pip install forgerypy
//import forgery_py

富文本文档编辑器markdown和flask-pagedown,以及HTML清理器 bleach

pip install markdown flask-pagedown bleach

Web服务测试工具httpie

pip install httpie

其他常用的flask扩展资源

flask与python相关资源

系统发布有关资源

上一篇下一篇

猜你喜欢

热点阅读