#!/usr/bin/env python """ This trivial WSGI app saves your name as a cookie. @author: Stephen Pascoe @requires: paste """ from paste.request import get_cookie_dict from paste.wsgiwrappers import WSGIResponse, WSGIRequest def cookieFactory(global_config,**local_conf): return cookieEG() class cookieEG(object): """ We have 3 cases for which this request might be invoked: 1. GET for the first time (no cookie set) 2. POST in response to filling in the form 3. GET after the cookie is set """ def __init__(self): pass def __call__(self, environ, start_response): self.request = WSGIRequest(environ) if self.request.POST: self.name = self.request.POST['hello_name'] else: try: self.name = self.request.cookies['hello_name'] except KeyError: self.name = None if self.name: iter = self.__say() else: iter = self.__ask() response = WSGIResponse(mimetype='text/html') if self.name: response.set_cookie('hello_name', self.name) response(self.request.environ, start_response) return iter def __say(self): yield """

Hello %s

""" % self.name def __ask(self): yield """

Hello

Who are you?

"""