Fun with 'str' and 'bytes'

Frank Millman frank at chagford.com
Sat Mar 5 03:04:19 EST 2011


On Mar 4, 6:40 pm, nn <prueba... at latinmail.com> wrote:
> On Mar 4, 7:32 am, "Frank Millman" <fr... at chagford.com> wrote:
>
> > Hi all
>
> > I want to create a cookie containing a session id. In python 2.6 I had the
> > following -
>
> > from __future__ import unicode_literals
> > session_id = b64encode(urandom(20))
> > response_headers.append(
> >     (b'Set-Cookie', b'sid="{0}"'.format(session_id)))
>
> > After upgrading to 3.2, the above lines generate this traceback -
> >     AttributeError: 'bytes' object has no attribute 'format'
>
> > The best workaround I can come up with is the following -
>
> > session_id = b64encode(urandom(20))
> > response_headers.append(
> >     (b'Set-Cookie', b'sid="' + session_id + b'"'))
>
> > It works, but it is not pretty. Is there a more elegant solution?
>
> > Thanks
>
> > Frank Millman
>
> As far as I know, that is pretty much it. Also see:
>
> http://bugs.python.org/issue3982http://mail.python.org/pipermail/python-dev/2010-July/102252.htmlhttp://lucumr.pocoo.org/2010/5/25/wsgi-on-python-3/

Thanks for the response, and for the links - interesting reading.

Frank



More information about the Python-list mailing list