如何判断python中的连接是否已死 [英] How to tell if a connection is dead in python

查看:33
本文介绍了如何判断python中的连接是否已死的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 python 应用程序能够知道另一端的套接字何时被丢弃.有这个方法吗?

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?

推荐答案

这取决于您所说的丢弃"是什么意思.对于 TCP 套接字,如果另一端通过close() 或进程终止,您将通过读取文件末尾或读取错误来发现,通常 errno 被设置为您的操作系统设置的任何对等连接重置".对于python,您将读取一个零长度的字符串,或者当您尝试从套接字读取或写入时会抛出一个socket.error.

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.

这篇关于如何判断python中的连接是否已死的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆