I have the following code:
def distributeMembersFile(members):
for node in members:
node = node.strip() # Strip trailing \n
if node == socket.hostname(): # No need to send the file to itself
continue
conn = None
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((node, port))
# TODO: Can possibly update the list of members here as well
s.sendall('DWLD')
s.recv(4)
except socket.error, msg:
logging.info(msg)
finally:
if s:
s.close()
Now, my question is that even though s.connect() is in a try except socket.error block, that exception is not being caught. I see the following traceback on console:
s.connect((node, port))
File "<string>", line 1, in connect
error: (111, 'Connection refused')
Interestingly, in other places I have the same try except socket.error block, and this particular (Connection refused) error is caught as:
INFO (111, 'Connection refused')
Above was printed by the logging.info function in except block. The only way I see right now to catch this exception is to use a 'bare' except, which is not considered a very good thing. Also, I found it peculiar that the error on the console is not being presented as
socket.error: (111, 'Connection refused')
Instead, it just says
error: (111. 'Connection refused')
missing the leading word 'socket'. What could be the reason behing the exception not being caught?