'Serialize the secure cookie into a string.
If expires is provided, the session will be automatically invalidated
after expiration when you unseralize it. This provides better
protection against session cookie theft.
:param expires: an optional expiration date for the cookie (a
:class:`datetime.datetime` object)'

def serialize(self, expires=None):

    if (self.secret_key is None):
        raise RuntimeError('no    secret    key    defined')
    if expires:
        self['_expires'] = _date_to_unix(expires)
    result = []
    mac = hmac(self.secret_key, None, self.hash_method)
    for (key, value) in sorted(self.items()):
        result.append(('%s=%s' % (url_quote_plus(key), self.quote(value).decode('ascii'))).encode('ascii'))
        mac.update(('|' + result[(-1)]))
    return '?'.join([base64.b64encode(mac.digest()).strip(), '&'.join(result)])
