python middleware for handling sessions