python_note
  • Introduction
  • My Python
    • Anaconda
    • argparse
    • datetime
    • json
    • logging
    • numpy
    • open
    • openCC
    • pandas & csv
    • Socket & SocketServer
    • re
    • yaml
    • smtp
    • 物件操作
    • unittest
    • 線程
    • prettytable
    • IO
    • pycurl
    • sys
    • pickle
    • auto-python-to-exe
    • cython
    • nuitka
  • Crawler
    • Urllib & Requests
      • User-agent
      • Percent-Encoding
      • mail code
    • Selenium
    • TCP & UDP
    • 控制字符(control character)
  • Web Development
    • Flask
      • RESTful api
      • Template
      • blueprint
    • Django
      • 環境佈署(windows)
    • 檢查Port
    • Apache
    • 使用者行為
    • jQuery
    • 壓力測試
    • DataTable
    • Bootstrap
    • CSS
    • JavaScript
    • Chart.js
  • Deep Learning
    • Keras 設定
    • RNN
    • LSTM
  • Test
    • T-Test
  • 資料結構
    • Hash
    • 時間複雜度
  • NLP
    • N-gram
    • CKIP
    • 中文轉數字
    • CRF
    • Mutual Information
    • 模糊比對
  • Linebot
    • Heroku
    • 圖文選單
    • channel
  • Linux
    • 常用指令
    • shell script
    • sshfs
    • ssh
    • nodejs & npm
    • debug
  • GCP
    • app engine
    • ssh(gcp)
    • gsutil
    • brabrabra
    • Load Balancer
    • k8s
  • Database
    • mysql
    • elasticsearch
      • Query
      • Backup and Restore
      • elasticdump
      • es2csv
      • ELK
    • mongodb
      • install
      • authentication
      • pymongo
    • sql server
  • go
    • Swarm
  • Docker
    • Kitematic
    • Dockerfile
    • Swarm
  • Git
  • 其他
    • USB軟體保護
    • Windows效能監視器
  • Blockchain
Powered by GitBook
On this page

Was this helpful?

  1. Web Development

Flask

PreviousWeb DevelopmentNextRESTful api

Last updated 5 years ago

Was this helpful?

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

則會導致驗證之敗

參考文章:

config設定

開發環境 vs 測試環境

thread與process之間的差異?

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

ImmutableMultiDict

下載檔案

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

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)

保護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

參考文章:

..待研究

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

https://flask123.sinaapp.com/article/40/
https://werkzeug.palletsprojects.com/en/0.16.x/utils/
https://medium.com/datainpoint/flask-web-api-quickstart-3b13d96cccc2
https://www.reddit.com/r/Python/comments/8bb102/why_shouldnt_one_use_flask_bottle_django_etc/
https://blog.csdn.net/Duke_Huan_of_Qi/article/details/76064225
https://stackoverflow.com/questions/13522137/in-flask-convert-form-post-object-into-a-representation-suitable-for-mongodb
https://blog.louie.lu/2017/08/07/secrets-python-standard-library-10/
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