How to tell if a connection is dead in python

前端 未结 5 631
忘掉有多难
忘掉有多难 2020-11-29 23:41

I want my python application to be able to tell when the socket on the other side has been dropped. Is there a method for this?

5条回答
  •  忘掉有多难
    2020-11-30 00:00

    It depends on what you mean by "dropped". For TCP sockets, if the other end closes the connection either through close() or the process terminating, you'll find out by reading an end of file, or getting a read error, usually the errno being set to whatever 'connection reset by peer' is by your operating system. For python, you'll read a zero length string, or a socket.error will be thrown when you try to read or write from the socket.

提交回复
热议问题