Catch “socket.error: [Errno 111] Connection refused” exception

匿名 (未验证) 提交于 2019-12-03 01:55:01

问题:

How could I catch socket.error: [Errno 111] Connection refused exception ?

try:     senderSocket.send("Hello") except ?????:     print "catch !"     

回答1:

By catching all socket.error exceptions, and re-raising it if the errno attribute is not equal to 111. Or, better yet, use the errno.ECONNREFUSED constant instead:

import errno from socket import error as socket_error  try:     senderSocket.send('Hello') except socket_error as serr:     if serr.errno != errno.ECONNREFUSED:         # Not the error we are looking for, re-raise         raise serr     # connection refused     # handle here


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