Flask

from werkzeug.security import generate_password_hash, check_password_hash


pw = 'test123'  # 原始密碼
pw_hash = generate_password_hash(pw)  # 加密後的密碼
print(len(pw_hash), pw_hash)

print(check_password_hash(pw_hash, pw))  # 驗證成功則回傳True

無論密碼內容與長短,加密結果格式皆相同:method:salt:hash

範例:pbkdf2:sha256:150000$9ymHYUlg$eb745944827e25cdaf804da896e3d1fb7347927f091415a294575ffec9fbc087

而且因為長度較長,必須注意資料庫密碼欄位是否足夠存取hash結果

若欄位太短,如:max_length=50

則會導致驗證之敗

參考文章: https://flask123.sinaapp.com/article/40/ https://werkzeug.palletsprojects.com/en/0.16.x/utils/

config設定

https://medium.com/datainpoint/flask-web-api-quickstart-3b13d96cccc2

開發環境 vs 測試環境

thread與process之間的差異?

https://www.reddit.com/r/Python/comments/8bb102/why_shouldnt_one_use_flask_bottle_django_etc/

在flask中使用jsonify和json.dumps的区别

https://blog.csdn.net/Duke_Huan_of_Qi/article/details/76064225

ImmutableMultiDict

https://stackoverflow.com/questions/13522137/in-flask-convert-form-post-object-into-a-representation-suitable-for-mongodb

下載檔案

若要同時載多個,建議使用壓縮檔(尚未測試)

session

session保存位置

SESSION_TYPE = ‘null’          : 采用flask默认的保存在cookie中;
SESSION_TYPE = ‘redis’         : 保存在redis中
SESSION_TYPE = ‘memcached’     : 保存在memcache
SESSION_TYPE = 'filesystem'      : 保存在文件
SESSION_TYPE = 'mongodb'        : 保存在MongoDB
SESSION_TYPE = 'sqlalchemy'     : 保存在关系型数据库

密碼設定可用secrets套件

app.config['SECRET_KEY'] = secrets.token_urlsafe(16)

https://blog.louie.lu/2017/08/07/secrets-python-standard-library-10/

保護cookie內容,設為true後記得設置secret_key

app.config['SESSION_USE_SIGNER'] = True

修改key的前綴內容

app.config['SESSION_KEY_PREFIX'] = str(os.urandom(24)) + ":"

設定session到期時間

app.config['PERMANENT_SESSION_LIFETIME'] = 180

cookie更新頻率,預設為true,表示每個request都會使cookie更新

app.config['SESSION_REFRESH_EACH_REQUEST'] = True

參考文章:

https://www.cnblogs.com/cwp-bg/p/9339865.html

http://docs.jinkan.org/docs/flask/config.html

https://dormousehole.readthedocs.io/en/latest/config.html

..待研究

from flask import session
session.permanent = True
session.modified = True

Last updated