Fun with 'str' and 'bytes'

Discussion in 'Python' started by Frank Millman, Mar 4, 2011.

  1. 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
     
    Frank Millman, Mar 4, 2011
    #1
    1. Advertising

  2. Frank Millman

    nn Guest

    On Mar 4, 7:32 am, "Frank Millman" <> 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/
     
    nn, Mar 4, 2011
    #2
    1. Advertising

  3. On Mar 4, 6:40 pm, nn <> wrote:
    > On Mar 4, 7:32 am, "Frank Millman" <> 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/issue3982htt.../lucumr.pocoo.org/2010/5/25/wsgi-on-python-3/


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

    Frank
     
    Frank Millman, Mar 5, 2011
    #3
  4. Frank Millman

    Terry Reedy Guest

    On 3/6/2011 4:55 PM, Nicholas Devenish wrote:
    > On 04/03/2011 16:40, nn wrote:
    >> As far as I know, that is pretty much it. Also see:
    >>
    >> http://bugs.python.org/issue3982

    >
    > That is a depressing bug report, and really comes across as people who
    > don't use networking commenting on the requirements of people who write
    > networking code.
    >
    > It's good to see that the idea was getting a bit more treatment last yeat.


    I added the following note to that issue.
    "
    struct.pack, not mentioned here, is a binary bytes formatting function.
    It can do ascii bytes mixed with binary octets. It works the same in
    Python 2 and 3.

    Str.bytes does two things: convert objects to strings according to the
    contents of field specifiers; interpolate the resulting strings into a
    template string according to the locations of the field specifiers. If
    desired bytes represent encoded text, then encoding computed text is the
    obvious Py3 solution.

    For some mixed ascii-binary uses, struct.pack is not as elegant as a
    bytes.format might be. But I think such a method should use struct
    format codes within field specifiers to convert objects into binary
    bytes rather than text.
    "
    Note that struct codes include s = C char[] = Py bytes of possibly
    unspecified length copied unchanged.

    --
    Terry Jan Reedy
     
    Terry Reedy, Mar 7, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    2
    Views:
    502
    Thomas G. Marshall
    Aug 3, 2003
  2. Andy Fish
    Replies:
    65
    Views:
    1,822
    Mabden
    May 18, 2004
  3. Sullivan WxPyQtKinter

    It is fun.the result of str.lower(str())

    Sullivan WxPyQtKinter, Mar 7, 2006, in forum: Python
    Replies:
    5
    Views:
    354
    Tim Roberts
    Mar 9, 2006
  4. dolphin
    Replies:
    4
    Views:
    337
    Jorgen Grahn
    Aug 25, 2007
  5. er
    Replies:
    2
    Views:
    541
Loading...

Share This Page