FAQ
I am trying to figure out how to send text or byte in python 3.1. I am
trying to send data to flash socket to get there. I am not sure how to
work it.

buff= 'id=' , self.id , ':balive=False\n'
clientSock.send(buff);

Search Discussions

  • Francesco Bochicchio at Jul 30, 2009 at 10:29 am

    On Jul 30, 5:52?am, NighterNet wrote:
    I am trying to figure out how to send text or byte in python 3.1. I am
    trying to send data to flash socket to get there. I am not sure how to
    work it.

    buff= 'id=' , self.id , ':balive=False\n'
    clientSock.send(buff);
    Try putting a 'b' before the constant string that you want to send:
    type(b'123')
    <class 'bytes'>

    or use something like this to convert non constant strings (with only
    ASCII characters) into bytes:
    s = "A non-constant string : %d " % n
    s
    'A non-constant string : 12 '
    type(s)
    <class 'str'>
    b = bytes ( ord(c) for c in s )
    b
    b'A non-constant string : 12 '

    You could also use struct.pack , that in python 3.x returns bytes and
    not strings. In this case you need to specify the size of the string
    (extra
    bytes are zero-filled):

    import struct
    struct.pack( "30s", "A non-constant string : %d " % n )
    b'A non-constant string : 12 \x00\x00\x00'



    Ciao
    -----
    FB
  • Jan Kaliszewski at Jul 30, 2009 at 8:16 pm

    30-07-2009 o 12:29:24 Francesco Bochicchio wrote:
    On Jul 30, 5:52?am, NighterNet wrote:
    I am trying to figure out how to send text or byte in python 3.1. I am
    trying to send data to flash socket to get there. I am not sure how to
    work it.

    buff= 'id=' , self.id , ':balive=False\n'
    clientSock.send(buff);
    Try putting a 'b' before the constant string that you want to send:
    type(b'123')
    <class 'bytes'>
    It's true. In python '...' literals are for strings ('str' type) and
    b'...' literals are for binary data (bytes type).

    Sockets' send() and recv() need binary data ('bytes' objects).
    or use something like this to convert non constant strings (with only
    ASCII characters) into bytes:
    s = "A non-constant string : %d " % n
    s
    'A non-constant string : 12 '
    type(s)
    <class 'str'>
    What???

    'str' type in Python 3.x IS NOT a type of "non-constant" strings and
    IS NOT a type of strings "with only ASCII characters"!

    'str' type in Python 3.x *is* the type of immutable ('constant') and
    Unicode character (Unicode, not only ASCII) strings. It's the same what
    'unicode' type in Python 2.x.

    'bytes' type objects in Python 3.x are also immutable. They are good
    for binary data (also text data encoded to binary data; see below).

    'bytearray' type objects are like 'bytes', but mutable (like e. g.
    lists).
    b = bytes ( ord(c) for c in s )
    b
    b'A non-constant string : 12 '

    You could also use struct.pack , that in python 3.x returns bytes and
    not strings. In this case you need to specify the size of the string
    (extra bytes are zero-filled):

    import struct
    What for all that weird actions???

    * Simply do:

    bytes_object = str_object.encode(<encoding>)
    or
    bytes_object = bytes(str_object, <encoding>)

    -- where <encoding> could be e.g. 'ascii', 'utf-8', 'iso-8859-1' etc.;
    using the former method, you can omit this argument -> then default
    encoding will be used (usualy 'utf-8').

    * As easily as that, you can convert bytes to str:

    str_object = bytes_object.decode(<encoding>)
    or
    str_object = str(bytes_object, <encoding>)

    * Some examples:
    'What makes you think she is a witch?' # it's str
    'What makes you think she is a witch?'
    'What makes you think she is a witch?'.encode() # to bytes
    b'What makes you think she is a witch?'
    'What makes you think she is a witch?'.encode('utf-8') # to bytes
    b'What makes you think she is a witch?'
    bytes('What makes you think she is a witch?') # to bytes???
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: string argument without an encoding
    bytes('What makes you think she is a witch?', 'ascii') # to bytes
    b'What makes you think she is a witch?'
    b'What makes you think she is a witch?'.decode() # to str
    'What makes you think she is a witch?'
    b'What makes you think she is a witch?'.decode('utf-8') # to str
    'What makes you think she is a witch?'
    str(b'What makes you think she is a witch?', 'utf-8') # to str
    'What makes you think she is a witch?'
    str(b'What makes you think she is a witch?') # but not this way
    "b'What makes you think she is a witch?'"
    [^ note this]

    Cheers,

    *j

    --
    Jan Kaliszewski (zuo) <zuo at chopin.edu.pl>
  • Francesco Bochicchio at Jul 31, 2009 at 7:51 am

    On Jul 30, 10:16?pm, "Jan Kaliszewski" wrote:
    30-07-2009 o 12:29:24 Francesco Bochicchio wrote:
    On Jul 30, 5:52?am, NighterNet wrote:
    I am trying to figure out how to send text or byte in python 3.1. I am
    trying to send data to flash socket to get there. I am not sure how to
    work it.
    buff= 'id=' , self.id , ':balive=False\n'
    clientSock.send(buff);
    Try putting a 'b' before the constant string that you want to send:
    type(b'123')
    <class 'bytes'>
    It's true. In python '...' literals are for strings ('str' type) and
    b'...' literals are for binary data (bytes type).

    Sockets' send() and recv() need binary data ('bytes' objects).
    or use something like this to convert non constant strings (with only
    ASCII characters) into bytes:
    s = "A non-constant string : %d " % n
    s
    'A non-constant string : 12 '
    type(s)
    <class 'str'>
    What???

    'str' type in Python 3.x IS NOT a type of "non-constant" strings and
    IS NOT a type of strings "with only ASCII characters"!

    'str' type in Python 3.x *is* the type of immutable ('constant') and
    Unicode character (Unicode, not only ASCII) strings. It's the same what
    'unicode' type in Python 2.x.
    ... unfortunate choice of words and not enough research on my part
    here. WHat I meant was: if you want
    to send via socket a constant string, use b"..."; if you want to send
    via socket a string that you
    made out of variables (the "non-constant string" ) then you have to
    convert it in bytes. Since I did not
    now of the encode method, I tried other solutions, like the one-liner
    using ord or using the struct
    module. Obviously, encode is better.

    My bad :-)

    Ciao
    -------
    FB
  • Mark Tolonen at Jul 30, 2009 at 1:56 pm
    "NighterNet" <darkneter at gmail.com> wrote in message
    news:55aba832-df6d-455f-bf34-04d37eb061cd at i4g2000prm.googlegroups.com...
    I am trying to figure out how to send text or byte in python 3.1. I am
    trying to send data to flash socket to get there. I am not sure how to
    work it.

    buff= 'id=' , self.id , ':balive=False\n'
    clientSock.send(buff);
    --
    http://mail.python.org/mailman/listinfo/python-list
    Python 3.1 strings are Unicode (think characters not bytes). When writing
    to files, sockets, etc. bytes must be used. Unicode strings have an
    encode() method, where you can specify an appropriate encoding (ascii,
    latin-1, windows-1252, utf-8, etc.):

    clientSock.send(buff.encode('ascii'))

    When reading from the socket, you can decode() the byte strings back into
    Unicode strings.

    data = clientSock.recv(1024).decode('ascii')

    -Mark
  • NighterNet at Jul 30, 2009 at 4:06 pm

    On Jul 30, 6:56?am, "Mark Tolonen" wrote:
    "NighterNet" <darkne... at gmail.com> wrote in message

    news:55aba832-df6d-455f-bf34-04d37eb061cd at i4g2000prm.googlegroups.com...
    I am trying to figure out how to send text or byte in python3.1. I am
    trying to send data to flashsocketto get there. I am not sure how to
    work it.
    buff= 'id=' , self.id , ':balive=False\n'
    clientSock.send(buff);
    --
    http://mail.python.org/mailman/listinfo/python-list
    Python3.1strings are Unicode (think characters not bytes). ?When writing
    to files, sockets, etc. bytes must be used. ?Unicode strings have an
    encode() method, where you can specify an appropriate encoding (ascii,
    latin-1, windows-1252, utf-8, etc.):

    ? ? clientSock.send(buff.encode('ascii'))

    When reading from thesocket, you can decode() the byte strings back into
    Unicode strings.

    ? ? data = clientSock.recv(1024).decode('ascii')

    -Mark
    I am not sure how to use struct package.
    Here an example for the input:
    {id:1,playername:guest,x:100,y:50}
    {id:2,playername:tester,x:100,y:50}

    struct.pack(? )
  • Francesco Bochicchio at Jul 30, 2009 at 5:03 pm

    On 30 Lug, 18:06, NighterNet wrote:
    On Jul 30, 6:56?am, "Mark Tolonen" wrote:




    "NighterNet" <darkne... at gmail.com> wrote in message
    news:55aba832-df6d-455f-bf34-04d37eb061cd at i4g2000prm.googlegroups.com...
    I am trying to figure out how to send text or byte in python3.1. I am
    trying to send data to flashsocketto get there. I am not sure how to
    work it.
    buff= 'id=' , self.id , ':balive=False\n'
    clientSock.send(buff);
    --
    http://mail.python.org/mailman/listinfo/python-list
    Python3.1strings are Unicode (think characters not bytes). ?When writing
    to files, sockets, etc. bytes must be used. ?Unicode strings have an
    encode() method, where you can specify an appropriate encoding (ascii,
    latin-1, windows-1252, utf-8, etc.):
    ? ? clientSock.send(buff.encode('ascii'))
    When reading from thesocket, you can decode() the byte strings back into
    Unicode strings.
    ? ? data = clientSock.recv(1024).decode('ascii')
    -Mark
    I am not sure how to use struct package.
    Here an example for the input:
    {id:1,playername:guest,x:100,y:50}
    {id:2,playername:tester,x:100,y:50}

    struct.pack(? )
    If your messages are ASCII, like it seems, forget about struct, which
    is for 'binary' message format.
    Format the string as you would in 2.6 ( using % or string.format for
    instance ) and then use encode
    as instructed.
  • Jan Kaliszewski at Jul 30, 2009 at 7:20 pm

    30-07-2009 o 05:52:06 NighterNet wrote:

    I am trying to figure out how to send text or byte in python 3.1. I am
    trying to send data to flash socket to get there. I am not sure how to
    work it.

    buff= 'id=' , self.id , ':balive=False\n'
    clientSock.send(buff);
    And what is the problem?

    *j

    --
    Jan Kaliszewski (zuo) <zuo at chopin.edu.pl>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 30, '09 at 3:52a
activeJul 31, '09 at 7:51a
posts8
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase