sending binary data over sockets

Diez B. Roggisch deets at nospam.web.de
Mon Jul 3 17:03:02 EDT 2006


Premature sending syndrome...

Diez B. Roggisch schrieb:
> thorley at gmail.com schrieb:
>> Greetings, since there was no reponse to my previous post about an
>> existing FastCGI server in python, I've taken to writing my own. (which
>> of course I'll share--*if* there's something to share ;)
>>
>> My problem now, is that I need to send certain binary data over a
>> socket. That is, I want to make some bytes, and stuff them in a TCP
>> packet, send them down the pipe, and then listen for a response.
>>
>> socket.send, as best I can tell, will only send strings. I've read on
>> the list other conversations where the recommendation was to use xdrlib
>> or struct. But it appears that is only useful when you control the
>> client and the server. PLEASE correct me if I'm wrong, but using struct
>> just encodes the binary data as a string, right?

Python strings are binary data and can contain
  - in oppostion to e.g. C-strings - null-bytes.

So it is perfectly alright to send "only" strings over a socket.

Diez



More information about the Python-list mailing list