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

后端 未结 1 972
春和景丽
春和景丽 2020-12-05 01:41

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

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


        
相关标签:
1条回答
  • 2020-12-05 02:25

    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
    
    0 讨论(0)
提交回复
热议问题