socket send

N

NighterNet

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);
 
F

Francesco Bochicchio

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:
<class 'bytes'>

or use something like this to convert non constant strings (with only
ASCII characters) into bytes:
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
b'A non-constant string : 12 \x00\x00\x00'



Ciao
 
M

Mark Tolonen

NighterNet said:
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);

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
 
N

NighterNet

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(? )
 
F

Francesco Bochicchio

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.
 
J

Jan Kaliszewski

30-07-2009 o 05:52:06 NighterNet said:
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
 
J

Jan Kaliszewski

30-07-2009 o 12:29:24 Francesco Bochicchio said:
Try putting a 'b' before the constant string that you want to send:

<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:
<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'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:
Traceback (most recent call last):
b'What makes you think she is a witch?'
"b'What makes you think she is a witch?'"
[^ note this]

Cheers,

*j
 
F

Francesco Bochicchio

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).


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,518
Latest member
TobiasAxf

Latest Threads

Top