Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

sockets

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

try:     senderSocket.send("Hello") except ?????:     print "catch !"      
like image 411
URL87 Avatar asked Jan 20 '13 14:01

URL87


People also ask

What does Errno 111 Connection refused mean?

For example, the classic Connection refused errno 111 means that the initial SYN packet to the host you connect() to was responded with an RST packet instead of the normal SYN+ACK — which usually happens when there's no program listening to the given port on the remote computer you connect() to.

What is socket error connection refused?

10061 is a Connection Refused error sent to you by the server. You could not make a connection because the target machine actively refused it. The most common cause is a misconfigured server, full server, or incorrect Port specified by the client.


1 Answers

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 
like image 94
Martijn Pieters Avatar answered Sep 22 '22 20:09

Martijn Pieters