Package: vishnu
Sessions for python WSGI applications.FeaturesCookie based session for python WSGI applicationsConfigurable for the following cookie settingsDomainPathSecureHttpOnlyExpires (timeout)Support for multiple backendsGoogle App Engine MemcacheGoogle App Engine NDBPython MemcachedPyMemcacheRedisHMAC signature to verify cookie has not been tampered withAutosave option which saves anytime a session value is modifiedOptional Encryption of cookie data using AESCustom timeout per sessionInstallationVishnu is available onPyPiand we recommend installation viapip.pipinstallvishnuThe following extra installations also exist which include the desired backend library as a requirement.pipinstallvishnu[pymemcache]pipinstallvishnu[python-memcached]pipinstallvishnu[redis]If you are working with Google App Engine we recommend installation viapipas avendored package.pipinstall-tlibvishnuEdit theappengine_config.pyfile and provide your library directory to thevendor.add()method.fromgoogle.appengine.extimportvendorvendor.add('lib')Alternatively download yourpreferred tagged releaseand all you should have to include is thevishnufolder.ConfigurationSession ConfigThe following parameters are available for session configuration.parameterrequireddefaulttypedescriptionsecretyesNonestringSecret used for HMAC signature, must be at least 32 characters.cookie_namenovishnustringName to use for cookie.encrypt_keynoNonestringKey used to encrypt cookie data, if omitted then data will not be encrypted.securenoTrueboolOnly send this cookie over SSLdomainnoN/AstringThe domain to set the cookie for, it omitted will use domain cookie was served from.pathno/stringThe path to set the cookie for, if omitted it will default to/http_onlynoTruestringA http-only cookie cannot be accessed by client-side APIs, such as JavaScriptauto_savenoFalseboolAutomatically save the session when a value is set.timeoutnoN/AintegerHow long until session/cookie expires, it omitted it will last for the length of the browser session.backendyesN/AbackendSeebackendsconfigurationExample of a session configuration.fromvishnu.sessionimportConfigfromvishnu.backendimportRedisconfig=Config(secret="your_secret",backend=Redis())WSGI MiddlewareTo use vishnu you must add it as a middleware to your WSGI application.fromvishnu.backendimportRedisfromvishnu.middlewareimportSessionMiddlewarefromvishnu.sessionimportConfigmy_config=Config(secret="your_secret",backend=Redis())app=SessionMiddleware(app=wsgi_app,config=my_config)BackendsGoogle App Engine (memcache)fromvishnu.backendimportGoogleAppEngineMemcacheconfig=Config(secret="your_secret",backend=GoogleAppEngineMemcache())Google App Engine (NDB)fromvishnu.backendimportGoogleAppEngineNDBconfig=Config(secret="your_secret",backend=GoogleAppEngineNDB())PyMemcacheparameterrequireddefaulttypehostnolocalhoststringportno11211integerfromvishnu.backendimportPyMemcacheconfig=Config(secret="your_secret",backend=PyMemcache(host="memcache.host",port=11222))PythonMemcachedparameterrequireddefaulttypehostnolocalhoststringportno11211integerfromvishnu.backendimportPythonMemcachedconfig=Config(secret="your_secret",backend=PythonMemcached())Redisparameterrequireddefaulttypehostnolocalhoststringportno6379integerdbno0integerfromvishnu.backendimportRedisconfig=Config(secret="your_secret",backend=Redis(host="redis.host",port=6421,db=0))Setting a Custom TimeoutEach session uses the default timeout specified in your server config but if you want to have particular sessions differ to this you can do the following.session=vishnu.get_session()session.timeout=3600session.save()The timeout is in seconds. To set the timeout to expire at the end of this session you can use thevishnu.session.TIMEOUT_SESSIONconstant.session=vishnu.get_session()session.timeout=vishnu.session.TIMEOUT_SESSIONsession.save()Cleaning up Expired Sessions (Google App Engine NDB backend only)Add the following to a cron handler.fromvishnu.utilimportgae_ndb_delete_expired_sessionswhilenotgae_ndb_delete_expired_sessions():passYou can alter the period after expired sessions are deleted by passing a value in seconds asdormant_for. You can also alter the amount of sessions to delete per call using thelimitargument.fromvishnu.utilimportgae_ndb_delete_expired_sessionswhilenotgae_ndb_delete_expired_sessions(dormant_for=3600,limit=100):pass