英文:
Python session on Google Cloud Run automatically logs out
问题
I am running a website on Google Cloud Run that I have written in Python. The site runs in a Docker image that uses python:latest
. The code works, however after some time the user is logged out and I dont know why the session does not last.
main.py:
"""
Main
File: main.py
Updated: 23.10.2022 Ditlefsen
"""
import os
import flask
from flask import request, session
# Flask
app = flask.Flask(__name__)
app.secret_key = os.urandom(12)
# - Index ------------------------------------------------------------------------------
@app.route('/', methods=['GET', 'POST'])
def __index():
# Login user
if not session.get('logged_in'):
request_form_global = request.form
inp_username = request_form_global['inp_username']
inp_password = request_form_global['inp_password']
if inp_username == "hello" and inp_password == "world":
session['logged_in'] = True
if session.get('logged_in'):
return "Welcome to the website", 200
else:
return "Access denied!", 304
# - Main ----------------------------------------------------------------------
def main(function_request: flask.wrappers.Request = None):
app.run(debug=False, host="0.0.0.0", port=8080)
if __name__ == '__main__':
main()
Dockerfile:
# Specify Python
FROM python:latest
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Open port
EXPOSE 8080
# Add Python script
RUN mkdir /app
WORKDIR /app
COPY . .
# Install dependencies
RUN pip install -r requirements.txt
# Set Python's path
ENV PYTHONPATH /app
# Run script
CMD [ "python", "./main.py" ]
requirements.txt:
cloud-sql-python-connector
flask
flask-cors
google-cloud-secret-manager
google
google-api-python-client
PyYAML
psycopg2-binary
pg8000
sqlalchemy==1.4.46
requests
flask-login
passlib
Werkzeug
What can I do to keep the user logged in?
英文:
I am running a website on Google Cloud Run that I have written in Python. The site runs in a Docker image that uses python:latest
. The code works, however after some time the user is logged out and I dont know why the session does not lasts.
main.py:
"""
Main
File: main.py
Updated: 23.10.2022 Ditlefsen
"""
import os
import flask
from flask import request, session
# Flask
app = flask.Flask(__name__)
app.secret_key = os.urandom(12)
# - Index ------------------------------------------------------------------------------
@app.route('/', methods=['GET', 'POST'])
def __index():
# Login user
if not session.get('logged_in'):
request_form_global = request.form
inp_username = request_form_global['inp_username']
inp_password = request_form_global['inp_password']
if inp_username == "hello" and inp_password == "world":
session['logged_in'] = True
if session.get('logged_in'):
return "Welcome to the website", 200
else:
return "Access denied!", 304
# - Main ----------------------------------------------------------------------
def main(function_request: flask.wrappers.Request = None):
app.run(debug=False, host="0.0.0.0", port=8080)
if __name__ == '__main__':
main()
Dockerfile:
# Specify Python
FROM python:latest
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Open port
EXPOSE 8080
# Add Python script
RUN mkdir /app
WORKDIR /app
COPY . .
# Install dependencies
RUN pip install -r requirements.txt
# Set Pythons path
ENV PYTHONPATH /app
# Run script
CMD [ "python", "./main.py" ]
requirements.txt:
cloud-sql-python-connector
flask
flask-cors
google-cloud-secret-manager
google
google-api-python-client
PyYAML
psycopg2-binary
pg8000
sqlalchemy==1.4.46
requests
flask-login
passlib
Werkzeug
What can I do to keep the user logged in?
答案1
得分: 0
- 如果您在本地测试代码,用户是否保持登录状态?
- 登出之前的延迟是多少?
- 我猜您的Google Cloud Run服务在一段时间后会关闭,这取决于您的订阅和/或参数(请参阅此处的配额)
英文:
- Does your user remain logged in if you test your code locally?
- What is the delay before being logged out?
My guess is that your Google Cloud Run service is shut off after a while, depending on your subscription and/or parameters (see quotas here)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论