Python: Persistent cookie, generate `expires` field
Question: I'm trying to generate the text for a persistent cookie in a simple Python web
application.

I'm having trouble finding a way to generate the `expires` field. The text
format for the field is somewhat complicated, and I'd rather not write code to
generate it myself.

Is there something in Python that will help? I've cooked at the docs for
`cookie` and `cookielib` and they seem to handle a lot of the cookie business,
except for generating the `expires` field


Answer: Use Python's
[`time.strftime()`](http://docs.python.org/library/time.html#time.strftime) to
get the time into the correct format (see [RFC 6265, section
5.1.1](http://tools.ietf.org/html/rfc6265#section-5.1.1)):

    
    
    >>> import time
    >>> expires = time.time() + 14 * 24 * 3600 # 14 days from now
    >>> time.strftime("%a, %d-%b-%Y %T GMT", time.gmtime(expires))
    'Sat, 16-Jul-2011 12:55:48 GMT'
    

(RFC 6265 indicates that the timezone is ignored, but all the examples have
"GMT", so I've put it here too. That's cargo-cult programming for you!)

