找回密碼
 註冊
搜索
查看: 97|回復: 0

[教學] (已解決)Cpanel Python Flask's session 無法正常運作

[複製鏈接]
發表於 前天 12:35 | 顯示全部樓層 |閱讀模式
Push to Facebook

部屬一個APP到CPANEL帳戶上
我發現一個問題在我的SERVER
我使用了.venv
這時我用session登入帳戶
卻跳出錯誤訊息


Cpanel Python Flask session 正常運作

Cpanel Python Flask session 正常運作


畫面秀出
Incomplete response received from application


Cpanel Python Flask session 正常運作

Cpanel Python Flask session 正常運作



我看了
  1. /var/log/apache2/error_log
複製代碼
發現是session沒有正常運作
  1. File "/home/flask/mvc-new/base/views.py", line 395, in Get_user_login
  2. App 2072846 output:     session['uid'] = result['uid']
  3. App 2072846 output:   File "/home/flask/mvc-new/.venv/lib64/python3.9/site-packages/flask/sessions.py", line 104, in _fail
  4. App 2072846 output:     raise RuntimeError(
  5. App 2072846 output: RuntimeError: The session is unavailable because no secret key was set.  Set the secret_key on the application to something unique and secret.
  6. App 2072846 output: 500 Internal Server Error: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
  7. App 2072846 output: [ pid=2072846, time=2025-09-03 22:34:16,267 ]: server error: 500 Internal Server Error: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application., route:https://flask.netyea.com/login
複製代碼

解決方法(使用MVC架構)
/var/log/apache2/error_log錯誤訊息
  1. App 2072846 output: RuntimeError: The session is unavailable because no secret key was set.  Set the secret_key on the application to something unique and secret.
複製代碼
是因為沒有在config寫入secret key

在__init__.py加入
  1. from flask import Flask

  2. app = Flask(__name__)
  3. app.secret_key = 'xxxxxx'
  4. app.config['SESSION_TYPE'] = 'filesystem'
  5. app.debug = True
複製代碼
APP正常運行



文章來源:NetYea 網頁設計

參考文章
https://support.cpanel.net/hc/en-us/requests/95781533?page=1
https://stackoverflow.com/questi ... k-session-extension

Cpanel Python Flask session 正常運作

Cpanel Python Flask session 正常運作

Cpanel Python Flask session 正常運作

Cpanel Python Flask session 正常運作
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

Archiver|手機版|小黑屋|TShopping

GMT+8, 2025-9-6 03:26 , Processed in 0.022011 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回復 返回頂部 返回列表