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

Walker Lindley brotherjenos at
Fri Jul 27 03:20:30 CEST 2007

The easiest way I've found is to just surround every socket-related thing
you do with try/except. the except statement is something like "except
socket.error, err_info". then the err_info variable will have a tuple
containing the BSD socket error number and the text of the error. You can
reference this with BSD socket information or just do trail and error to see
which exceptions you get when the client-side closes versus when the server
side closes. I'm not actually sure which they are, but I know the networked
program I'm working with uses this method to detect when a socket I'm doing
some operation on has been closed. I hope that helps some.


On 7/26/07, Jay Loden <python at> wrote:
> Roy Smith wrote:
> > In article <1185371863.622473.205010 at>,
> >  billiejoex <gnewsg at> wrote:
> >
> >> Hi there.
> >> I'm setting up test suite for a project of mine.
> >> >From test suite, acting as a client, I'd like to know, in certain
> >> situations, if the socket is closed on the other end or not.
> >> I noticed that I can "detect" such state if a call to
> >> returns 0 but it seems a little poor to me. :-\
> >> Is there a reliable way to test such socket 'state'?
> >
> > 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?
> >
> > 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.
> Thanks for the interesting information and suggestion of using select().
> You are correct that this is actually mostly a socket API question but
> pertains to Python since the code is all Python's socket and asyncore
> modules. It might help to step back and explain the original problem. The
> goal of this portion of the test suite we are writing for the project is to
> determine if a remote server is behaving properly by closing a socket from
> the server side based on a client-side command.
> Really what's needed is a way to make sure the socket gets closed, and
> preferably determine if it was closed from the remote end as expected. Do
> you know if this is possible to determine from the client side
> reliably/accurately? Would select()'s exceptional condition flag actually
> indicate whether or not the root cause of the condition was a socket closed
> by the remote peer? I've read through the select's manpage and I can't seem
> to find a reference that indicates what the possible values are for the I/O
> descriptor sets returned by select. Is there another man page, or a place in
> the header file for select I can look?
> Thanks for your help,
> -Jay
> --

This e-mail is licensed under the Creative Commons
Attribution-NoDerivs 2.5License. To view a copy of this license, visit or send a letter to Creative
Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list