Pyramid framework tips : Beaker session factory settings

In Pyramid framework there is default session factory as shown below:

# default session factory, not secure, use pyramid beaker add-on
from pyramid.session import UnencryptedCookieSessionFactoryConfig
session_factory = UnencryptedCookieSessionFactoryConfig("mysession")

It is unencrypted, detailsĀ  can be found in Pyramid docs.

There is pyramid_beaker add-on package as alternative. Beaker is a library for caching and sessions for use with web applications and stand-alone Python scripts and applications. For pyramid_beaker settings:

Install pyramid_beaker add-on package with easy_install:

$ easy_install pyramid_beaker

In development.ini and production.ini:

[app:main]
...
pyramid.includes = pyramid_debugtoolbar
                                   pyramid_tm
                                   pyramid_beaker                   

# pyramid_beaker add-on settings
session.type = file
session.data_dir = %(here)s/data/sessions/data
session.lock_dir = %(here)s/data/sessions/lock
session.key = customerskey
session.secret = customerssecret
session.cookie_on_exception = true
...

in Pyramid Application in __init__.py:

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    # default session factory, not secure, use pyramid beaker
    # session_factory = UnencryptedCookieSessionFactoryConfig("mysession")

    # pyramid_beaker add-on
    session_factory = session_factory_from_settings(settings)

    config = Configurator(
        settings=settings,
        session_factory=session_factory
    )
  ...

One Comment

  1. George
    Aug 25, 2014 @ 05:39:11

    Event it’s 2 years past, it’s still helpful for me, thanks bro~

    Reply

Leave a Reply