Sockets

D

Dan

I'm using the socket module and have run into a problem.

I want to send binary data over the connection. With C, this is easy:

write(sock_fd, data, length);

But it appears that the Python socket module has no method to send
binary data, it only sends strings. So there's no argument to tell it
the length, it stops when it gets the end of string character (null).

The only way I can see around it is to encode the data, but this is
really just an inelegant hack.

Any suggestions? Python is still new to me and I can't say that I've
mastered all its nuances.

Dan
 
J

jepler

Python strings always carry their length, and can have embedded NULs.
s.write("\0\1\2\3")
should write 4 bytes to the socket 's'.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCVfHbJd01MZaTXX0RAuk6AKClnCJS5NFK+I/et0RSWlmloJSAfwCaAjqg
t+Mb1SsJsfPnx4uaL/HQMXc=
=q0Td
-----END PGP SIGNATURE-----
 
G

Grant Edwards

But it appears that the Python socket module has no method to send
binary data, it only sends strings.

Grasshopper, things are often not what they appear. Likewise,
they often appear to be what they are not. Strings and binary
data are one and the same.
So there's no argument to tell it the length, it stops when it
gets the end of string character (null).

Nope. You're thinking of C strings. Python strings aren't
terminated with a null. A python string can contain 0x00
bytes. Think of Python strings as fixed length arrays of octets
(AKA bytes). [I'm talking about "normal" strings. There are
also unicode strings, but thinking about them will just make
your head hurt.]

You should probably study the "struct" module. I think that
will answer most of your questions:

http://docs.python.org/lib/module-struct.html

Since you're doing network stuff, pay particular attention to
the sections on byte order and alignment.

You never want to use native byte order and alignment on the
wire or on disk. Even if you think that the two machines will
always be the same, someday they won't be. You, of course,
won't be there at that point, and somebody will spend a week
and half tacking down the problem and swearing at you. Been
there, done that -- at least the tracking/swearing part.
Probably the other part too.
 
D

Dan

Python strings always carry their length, and can have embedded NULs.
s.write("\0\1\2\3")
should write 4 bytes to the socket 's'.

I'm taking binary data from a database, so it's not really a Python
string. Is there an easy way to convert it?

Thanks

Dan
 
D

Dave Brueck

Dan said:
>
>I'm taking binary data from a database, so it's not really a Python
>string. Is there an easy way to convert it?

Unless I'm misunderstanding you, you don't need to. It's not that Python
normal strings are "encoded" in some special format, it's just that a
string object does not make a distrinction between what is traditionally
called "text" and "binary data".

Play around with this at a Python prompt to see for yourself. For example:
>>> img = file('foo.jpg', 'rb').read() # use a real image filename though
>>> img[:50]
'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00H\x00H\x00\x00\xff\xdb\x00C\x00
\x05\x03\x04\x04\x04\x03\x05\x04\x04\x04\x05\x05\x05\x06\x07\x0c\x08\x07\x07\x07
\x07\x0f\x0b\x0b\t'

So, the fact that you're getting the data from a database is probably
immaterial.

HTH,
Dave
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top