common approach in socket programming

Peter Hansen peter at engcorp.com
Fri Jun 7 08:12:57 EDT 2002


Uwe Mayer wrote:
> 
> now my problem is how to know that
> 
> a) a connection is still open
> 
> i couldn't find a opened() or isopen() function or a similar attribute
> in the documentation. not for the socket object. for the file object
> there should be the open attribute.

It's open until it is closed, at which point a blocking recv() will 
return "" (once).

> b) how do i know the size of each packet?

You do not!  There are no guarantees in TCP/IP that the original
send() arrives in a single recv().  Data can be split, combined,
delayed in many ways.  You _must_ have some kind of termination
sequence (e.g. \r\n) to identify the end of a sequence, or you
must send the length ahead of time so you know how much more data
to read (e.g. "net strings").

Unless you are trying to do some really low level stuff where
you actually care about the individual packets, in which case 
you do not care about SocketServer (or probably sockets, either).

-Peter



More information about the Python-list mailing list