Set a response cookie.

        Parameters:

        key
          : The cookie name.
        value
          : The cookie value.
        max_age
          : The maximum age of the cookie in seconds, or as a
            datetime.timedelta object.
        path
          : Restrict the cookie to this path (default: '/').
        domain
          : Restrict the cookie to his domain.
        secure
          : When True, instruct the client to only sent the cookie over HTTPS.
        httponly
          : When True, instruct the client to disallow javascript access to
            the cookie.
        expires
          : Another way of specifying the maximum age of the cookie. Accepts
            the same values as max_age (number of seconds, datetime.timedelta).
            Additionaly accepts a datetime.datetime object.
            Note: a value of type int or float is interpreted as a number of
            seconds in the future, *not* as Unix timestamp.

def set_cookie(self, key, value='', max_age=None, path='/', domain=None,
                   secure=False, httponly=False, expires=None):
        """Set a response cookie.

        Parameters:

        key
          : The cookie name.
        value
          : The cookie value.
        max_age
          : The maximum age of the cookie in seconds, or as a
            datetime.timedelta object.
        path
          : Restrict the cookie to this path (default: '/').
        domain
          : Restrict the cookie to his domain.
        secure
          : When True, instruct the client to only sent the cookie over HTTPS.
        httponly
          : When True, instruct the client to disallow javascript access to
            the cookie.
        expires
          : Another way of specifying the maximum age of the cookie. Accepts
            the same values as max_age (number of seconds, datetime.timedelta).
            Additionaly accepts a datetime.datetime object.
            Note: a value of type int or float is interpreted as a number of
            seconds in the future, *not* as Unix timestamp.
        """
        key, value = key.encode('utf-8'), value.encode('utf-8')
        cookie = SimpleCookie({key: value})
        m = cookie[key]
        if max_age is not None:
            if isinstance(max_age, timedelta):
                m['max-age'] = int(total_seconds(max_age))
            else:
                m['max-age'] = int(max_age)
        if path is not None: m['path'] = path.encode('utf-8')
        if domain is not None: m['domain'] = domain.encode('utf-8')
        if secure: m['secure'] = True
        if httponly: m['httponly'] = True
        if expires is not None:
            # 'expires' expects an offset in seconds, like max-age
            if isinstance(expires, datetime):
                expires = total_seconds(expires - datetime.utcnow())
            elif isinstance(expires, timedelta):
                expires = total_seconds(expires)
            m['expires'] = int(expires)

        self.headers.add_header('Set-Cookie', m.OutputString())