Savagefool
Savagefool

Reputation: 223

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?

Upvotes: 8

Views: 13889

Answers (2)

Melroy van den Berg
Melroy van den Berg

Reputation: 3215

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

Upvotes: 6

Eric
Eric

Reputation: 97641

Your issue is with the syntax, not socket.error:

This python 2 code is deprecated:

except Exception, e:

In favor of

except Exception as e:

So you want:

except socket.error as err:

Upvotes: 9

Related Questions