When does socket.recv() raise an exception?

大憨熊 提交于 2020-02-05 07:00:08

问题


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.


回答1:


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"


来源:https://stackoverflow.com/questions/27568995/when-does-socket-recv-raise-an-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!