'Python 3.4, socket.error deprecated, new equivalent?

Originally the code was written like this:

except socket.error, err:
    print('Socket connection error... Waiting 10 seconds to retry.')
    del self.sock
    time.sleep(10)
    try_count += 1

The intention is to catch a socket connection error, this used to be err, or something similar.

However I have seen on a previous answer that socket.error has been deprecated from 2.6 onwards.

I can also confirm that 3.4 flags an error that says it does not support this syntax.

Does anyone please know the 3.4 equivalent?



Solution 1:[1]

Indeed socket.error is deprecated in Python 3. You can now catch the superclass (OSError). And if want you can check within the except which kind of subclass of the exception was really raised (like ECONNREFUSED).

try:
    ...
except OSError as e:
    ...

See: https://docs.python.org/3/library/exceptions.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1