Fun with 'str' and 'bytes'

nn pruebauno at latinmail.com
Fri Mar 4 11:40:47 EST 2011


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/issue3982
http://mail.python.org/pipermail/python-dev/2010-July/102252.html
http://lucumr.pocoo.org/2010/5/25/wsgi-on-python-3/



More information about the Python-list mailing list