Sunday, November 27, 2011

Read and write cookies in mod_wsgi application

Read a cookie from HTTP header:
def application(environ, start_response):
    import Cookie
    cookie = Cookie.SimpleCookie()
    cookie.load(environ['HTTP_COOKIE'])
    access_token = cookie['access_token'].value
Write a cookie to HTTP header:
def application(environ, start_response):
    import Cookie
    cookie = Cookie.SimpleCookie()
    cookie['access_token'] = '1xRD3WXsdfsw23rjfsj9L'
    access_token = cookie['access_token'].value
    start_response('200 OK', ('Set-Cookie',cookie["access_token"].OutputString())])

2 comments:

  1. Sorry but I couldn't get the blog, can you tell us what is this blog all about? Looking forward to hear from you, thanks

    ReplyDelete
  2. Thanks for taking the time to write this, back in 2011. I used it today in a project.

    ReplyDelete