部屬一個APP到CPANEL帳戶上
我發現一個問題在我的SERVER
我使用了.venv
這時我用session登入帳戶
卻跳出錯誤訊息
Cpanel Python Flask session 正常運作
畫面秀出
Incomplete response received from application
Cpanel Python Flask session 正常運作
我看了
- /var/log/apache2/error_log
複製代碼 發現是session沒有正常運作
- File "/home/flask/mvc-new/base/views.py", line 395, in Get_user_login
- App 2072846 output: session['uid'] = result['uid']
- App 2072846 output: File "/home/flask/mvc-new/.venv/lib64/python3.9/site-packages/flask/sessions.py", line 104, in _fail
- App 2072846 output: raise RuntimeError(
- 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.
- 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.
- 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錯誤訊息
- 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加入
- from flask import Flask
- app = Flask(__name__)
- app.secret_key = 'xxxxxx'
- app.config['SESSION_TYPE'] = 'filesystem'
- app.debug = True
複製代碼 APP正常運行
文章來源:NetYea 網頁設計
參考文章
https://support.cpanel.net/hc/en-us/requests/95781533?page=1
https://stackoverflow.com/questi ... k-session-extension
|