Viesturs
Viesturs

Reputation: 1691

How to access all active sessions in CherryPy

I want to restrict number of sessions that are logged in as admins (to 1).

I would like to just go through the list of active sessions and check if they are logged in as admin. This way I don't have to track login, logout, sessions expiring. Unfortunately there seems no obvious way to do that.

I know there are concurrency issues, but creating a simple lock around the code is simple enough.

Using cherrypy 3.2.I'm using in-memory session store.

Upvotes: 2

Views: 838

Answers (1)

Viesturs
Viesturs

Reputation: 1691

Found a way using some of CherryPy internal structres:

for id, session in cherrypy.session.cache.items():
  if session[0].get("login") == "admin":
    admin_count += 1

Where "login" is any session parameter.

Upvotes: 4

Related Questions