Sockets: Sending/receiving arbitrary amounts of data
donn at u.washington.edu
Wed May 2 19:29:20 CEST 2001
Quoth Greg Ewing <see at my.signature>:
| "Doobee R. Tzeck" wrote:
|> Considering Python is a high level Language I ask myself
|> if there shouldend be a way in Python to optionally shield the
|> user (programmer) from the hassle of handling of short socket
| I think so, too. I once suggested that file.read(),
| socket.recv() and similar methods should have an
| optional two-argument form:
| recv(min, max)
| This would block until at least min bytes had
| arrived, and return at most max bytes. If EOF
| occurs before min bytes have arrived, an exception
| is raised.
| Putting min == 0 gives the current behaviour.
| Putting min == max gives you a fixed-length read.
| Perhaps I'll PEP this one day, too.
That kind of interface ought to be implemented in Python, as
opposed to hacking special behaviors into fileobject.c and
socketmodule.c. I don't know if Python is a high level language
or not, but whatever it is, its straightforward support for a
lot of C operating system APIs like sockets seems to have worked
out pretty well.
The fileobject read() function isn't really all that similar,
to my eye, to the socket object recv() function, but posix.read
is about the same (literally the same on platforms with real
Berkeley sockets, where you can posix.read(sock.fileno(), size).)
Likewise for socket object send() and posix.write.
It appears to me most programmers (unlike myself and apparently
the original poster) prefer a buffered I/O interface. The stdio
read() will re-block forever trying to get that nth byte, so it
would seem to be more or less what you want. The buffer obviates
the "max" parameter. But it would be better in this case to have
read() with no parameter mean the same as posix.read() with the
default buffer size, instead of "read all data until end of file."
It would be nice to have a timeout, instead of "forever". It would
be nice to be able to see into the buffer when you want to do
something like select(), or when you want to report I/O status for
a progress indicator or something.
Donn Cave, donn at u.washington.edu
More information about the Python-list