bieffe62 at gmail.com
Fri Jul 31 03:51:45 EDT 2009
On Jul 30, 10:16 pm, "Jan Kaliszewski" <z... at chopin.edu.pl> wrote:
> 30-07-2009 o 12:29:24 Francesco Bochicchio <bieff... at gmail.com> wrote:
> > On Jul 30, 5:52 am, NighterNet <darkne... at gmail.com> 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'>
> '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 :-)
More information about the Python-list