socket.recv()何时引发异常? [英] When does socket.recv() raise an exception?

查看:252
本文介绍了socket.recv()何时引发异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用阻塞套接字,而我对recv()感到很困惑,因为我找不到关于它的任何体面的文档.正式的人似乎仅限于我.使我感到困惑的另一件事是,互联网(以及该站点上的)上的人们说,它不会引发任何阻止套接字的异常,而只是一直在等待.如果我残酷地关闭服务器,它将抛出Errno 10054(socket.error).

I'm using blocking sockets and I'm quite confused about recv() because I didn't find any decent documentation about it. The official one seems restricted to me. The other thing that is confusing me is that people on the internet(and also on this site) says that it doesn't raise any exception for blocking sockets, but it simply keeps waiting. If I brutally close the server it throws an Errno 10054(socket.error) instead.

推荐答案

简单地说,只要基础套接字操作失败,recv就会引发异常.问题在于它取决于您所使用的操作系统.在这里我可以猜测您在Windows上,因为10054是

To put it simply, recv will throw an exception whenever the underlying socket operation fail. The problem is that it depends on the OS you are on. Here I can guess that you are on Windows, because 10054 is a Windows error code for connection reset. Fortunately, when and why socket operations fail is roughly consistent between OSes (check the Windows documentation for details), and python has a portable solution to identify errors :

import errno
...
if err == errno.ECONNRESET :
    print "connection reset"

这篇关于socket.recv()何时引发异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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