python flask_session/sessions.py TypeError: '<=' not supported between instances of 'NoneType' and 'datetime.datetime'

69 Views Asked by At

I am trying to use mongodb for session persistence via flask_session but I get the error at the end of this message.

There is no such error when I use redis as the session data store.

In webapp.py I have the following configuration.

app.config["SESSION_PERMANENT"] = False;
app.config["SESSION_USE_SIGNER"] = True;
SESSION_TYPE="mongodb";
if(SESSION_TYPE=="redis"):
    # Configure Redis for storing the session data on the server-side
    app.config["SESSION_TYPE"] = "redis";
    #redis://:[password]@[host_url]:[port]
    app.config["SESSION_REDIS"] = redis.from_url("redis://{0}:{1}".format(some_web_conf_dict["REDIS_HOST"],int(some_web_conf_dict["REDIS_PORT"])));
elif(SESSION_TYPE=="mongodb"):
    # Configure Mongodb for storing the session data on the server-side
    app.config["SESSION_TYPE"] = "mongodb";
    mongoclient=pymongo.MongoClient(some_web_conf_dict["MONGODB_HOST"],int(some_web_conf_dict["MONGODB_PORT"]));
    app.config["SESSION_MONGODB"] = mongoclient;

app.secret_key = some_web_conf_dict["SECRET_KEY"];

I am running Python 3.12. The output of pip list is shown below.

Package            Version
------------------ ---------
blinker            1.6.2
cachelib           0.10.2
certifi            2023.7.22
charset-normalizer 3.3.0
click              8.1.7
Flask              2.3.3
Flask-PyMongo      2.3.0
Flask-Session      0.5.0
gunicorn           21.2.0
idna               3.4
itsdangerous       2.1.2
Jinja2             3.1.2
MarkupSafe         2.1.3
packaging          23.2
pika               1.3.2
pip                23.2.1
psycopg2           2.9.8
pyasn1             0.5.0
pyasn1-modules     0.3.0
pymongo            3.13.0
python-dateutil    2.8.2
python-ldap        3.4.3
redis              5.0.1
requests           2.31.0
setuptools         68.2.2
six                1.16.0
urllib3            2.0.6
Werkzeug           2.3.7
wheel              0.41.2
INFO  processes_id:10 time:2023-10-03 14:02:41,077 werkzeug   [/usr/local/lib/python3.12/site-packages/werkzeug/_internal.py _internal _log             187 ]: 10.0.0.2 - - [03/Oct/2023 14:02:41] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 2213, in __call__
    return self.wsgi_app(environ, start_response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 2193, in wsgi_app
    response = self.handle_exception(e)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 2189, in wsgi_app
    ctx.push()
  File "/usr/local/lib/python3.12/site-packages/flask/ctx.py", line 377, in push
    self.session = session_interface.open_session(self.app, self.request)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/flask_session/sessions.py", line 415, in open_session
    if document and document.get('expiration') <= datetime.utcnow():
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: '<=' not supported between instances of 'NoneType' and 'datetime.datetime'
INFO  processes_id:10 time:2023-10-03 14:02:41,159 werkzeug   [/usr/local/lib/python3.12/site-packages/werkzeug/_internal.py _internal _log             187 ]: 10.0.0.2 - - [03/Oct/2023 14:02:41] "GET /?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
INFO  processes_id:10 time:2023-10-03 14:02:41,161 werkzeug   [/usr/local/lib/python3.12/site-packages/werkzeug/_internal.py _internal _log             187 ]: 10.0.0.2 - - [03/Oct/2023 14:02:41] "GET /?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
INFO  processes_id:10 time:2023-10-03 14:02:41,202 werkzeug   [/usr/local/lib/python3.12/site-packages/werkzeug/_internal.py _internal _log             187 ]: 10.0.0.2 - - [03/Oct/2023 14:02:41] "GET /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
INFO  processes_id:10 time:2023-10-03 14:02:41,217 werkzeug   [/usr/local/lib/python3.12/site-packages/werkzeug/_internal.py _internal _log             187 ]: 10.0.0.2 - - [03/Oct/2023 14:02:41] "GET /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 304 -
0

There are 0 best solutions below