How to tell when a socket is closed on the other end?

billiejoex gnewsg at gmail.com
Wed Jul 25 11:04:58 EDT 2007


On 25 Lug, 16:37, Roy Smith <r... at panix.com> wrote:

> This isn't really a Python question, it's a Berkeley Socket API question.  
> You don't say, but I assume you're talking about a TCP (i.e. SOCKSTREAM)
> connection?

Yes.

> The answer is you can use the select() system call to detect "exceptional
> conditions" on a socket.  Python's select module provides this
> functionality, but to understand how to use it, you need to study the
> underlying API.
>
> On the other hand, socket.read() returning 0 works too.  What do you find
> "poor" about that?  What do you want to know about the connection being
> closed that you don't find out by getting 0 back from read()?

'poor' because it's 'tricky', since that send/write() and recv/read()
should be used for other tasks...
As far as I can tell this works on Linux and Windows, but I don't know
on other platforms.




More information about the Python-list mailing list