On 27/05/2016 02:51 μμ, Steven D'Aprano wrote: > On Fri, 27 May 2016 10:24 pm, Pavlos Parissis wrote: > >> Hi, >> >> So, we have ConnectionRefusedError in Python3 but not in Python2. >> Six module doesn't provide a wrapper about this. >> >> What is most efficient way to handle this situation in a try-catch block? > > Um, could you give us a hint as to context? >
The following is inside a while true loop which implements a
retry logic.
try:
unix_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
unix_socket.settimeout(0.5)
unix_socket.connect(self.socket_file)
unix_socket.send(six.b(command + '\n'))
file_handle = unix_socket.makefile()
data = file_handle.read().splitlines()
except ConnectionRefusedError:
raised = SocketConnectionError(self.socket_file)
except socket.timeout:
raised = SocketTimeout(socket_file=self.socket_file)
except OSError as error:
if error.errno == 106:
raised = SocketTransportError(socket_file=self.socket_file)
else:
raised = error
else:
procecess(data)
finally:
unix_socket.close()
if raised:
time.sleep(self.retry_interval)
> What does Python 2 raise instead of ConnectionRefusedError? I don't know, so
> for the sake of calling it *something* I'll say it's FooError. Then if it
> is a simple name, you can do this:
>
>
> if sys.version < "3":
> ConnectionRefusedError = FooError
>
> # much later
> try:
> do_the_thing()
> except ConnectionRefusedError:
> ...
>
>
>
> Or instead of checking for the version, you can check for the name:
>
>
> try:
> from some_module import ConnectionRefusedError
> except ImportError:
> # Must be Python 2
> from another_module import FooError as ConnectionRefusedError
>
>
> If neither of these techniques help, please give more context and we can be
> more specific.
>
>
>
I will try the approach mentioned by Ben Finney as I am already checking
'errno'.
Thanks everyone for the replies.
Cheers,
Pavlos
signature.asc
Description: OpenPGP digital signature
-- https://mail.python.org/mailman/listinfo/python-list
