Python在Google Cloud Run上的会话会自动注销。

huangapple go评论92阅读模式
英文:

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:

  1. """
  2. Main
  3. File: main.py
  4. Updated: 23.10.2022 Ditlefsen
  5. """
  6. import os
  7. import flask
  8. from flask import request, session
  9. # Flask
  10. app = flask.Flask(__name__)
  11. app.secret_key = os.urandom(12)
  12. # - Index ------------------------------------------------------------------------------
  13. @app.route('/', methods=['GET', 'POST'])
  14. def __index():
  15. # Login user
  16. if not session.get('logged_in'):
  17. request_form_global = request.form
  18. inp_username = request_form_global['inp_username']
  19. inp_password = request_form_global['inp_password']
  20. if inp_username == "hello" and inp_password == "world":
  21. session['logged_in'] = True
  22. if session.get('logged_in'):
  23. return "Welcome to the website", 200
  24. else:
  25. return "Access denied!", 304
  26. # - Main ----------------------------------------------------------------------
  27. def main(function_request: flask.wrappers.Request = None):
  28. app.run(debug=False, host="0.0.0.0", port=8080)
  29. if __name__ == '__main__':
  30. main()

Dockerfile:

  1. # Specify Python
  2. FROM python:latest
  3. ENV PYTHONDONTWRITEBYTECODE 1
  4. ENV PYTHONUNBUFFERED 1
  5. # Open port
  6. EXPOSE 8080
  7. # Add Python script
  8. RUN mkdir /app
  9. WORKDIR /app
  10. COPY . .
  11. # Install dependencies
  12. RUN pip install -r requirements.txt
  13. # Set Python's path
  14. ENV PYTHONPATH /app
  15. # Run script
  16. CMD [ "python", "./main.py" ]

requirements.txt:

  1. cloud-sql-python-connector
  2. flask
  3. flask-cors
  4. google-cloud-secret-manager
  5. google
  6. google-api-python-client
  7. PyYAML
  8. psycopg2-binary
  9. pg8000
  10. sqlalchemy==1.4.46
  11. requests
  12. flask-login
  13. passlib
  14. 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:

  1. """
  2. Main
  3. File: main.py
  4. Updated: 23.10.2022 Ditlefsen
  5. """
  6. import os
  7. import flask
  8. from flask import request, session
  9. # Flask
  10. app = flask.Flask(__name__)
  11. app.secret_key = os.urandom(12)
  12. # - Index ------------------------------------------------------------------------------
  13. @app.route('/', methods=['GET', 'POST'])
  14. def __index():
  15. # Login user
  16. if not session.get('logged_in'):
  17. request_form_global = request.form
  18. inp_username = request_form_global['inp_username']
  19. inp_password = request_form_global['inp_password']
  20. if inp_username == "hello" and inp_password == "world":
  21. session['logged_in'] = True
  22. if session.get('logged_in'):
  23. return "Welcome to the website", 200
  24. else:
  25. return "Access denied!", 304
  26. # - Main ----------------------------------------------------------------------
  27. def main(function_request: flask.wrappers.Request = None):
  28. app.run(debug=False, host="0.0.0.0", port=8080)
  29. if __name__ == '__main__':
  30. main()

Dockerfile:

  1. # Specify Python
  2. FROM python:latest
  3. ENV PYTHONDONTWRITEBYTECODE 1
  4. ENV PYTHONUNBUFFERED 1
  5. # Open port
  6. EXPOSE 8080
  7. # Add Python script
  8. RUN mkdir /app
  9. WORKDIR /app
  10. COPY . .
  11. # Install dependencies
  12. RUN pip install -r requirements.txt
  13. # Set Pythons path
  14. ENV PYTHONPATH /app
  15. # Run script
  16. CMD [ "python", "./main.py" ]

requirements.txt:

  1. cloud-sql-python-connector
  2. flask
  3. flask-cors
  4. google-cloud-secret-manager
  5. google
  6. google-api-python-client
  7. PyYAML
  8. psycopg2-binary
  9. pg8000
  10. sqlalchemy==1.4.46
  11. requests
  12. flask-login
  13. passlib
  14. 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)

huangapple
  • 本文由 发表于 2023年5月10日 14:08:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定