views:

268

answers:

1

I use python 2.4.1 on Linux, and a python package written inside the company I work in, for establishing a connection between 2 hosts for test purposes.

Upon establishing the connection the side defined as the client side failed when calling socket.connect with the correct parameters (I checked) with the error code 111. After searching the web for this error means, I learned that it means that the connection was actively refused.

But the code in the package for establishing the connection is supposed to deal with it, only it knows 10061 as the error code for this same error: The connection is refused.

Could it be that there are identical error codes for the same logical errors? Could it be that 111 is a system error of the Linux OS, as 10061 is python's or even another OS? Even so, isn't the entire concept of error codes to unify the logical errors with the same codes?

Should I simply add the 111 error code to the handling condition?

+5  A: 

It appears Python is exposing the error code from the OS - the interpretation of the code is OS-dependent.

111 is ECONNREFUSED on many Linux systems, and on Cygwin.

146 is ECONNREFUSED on Solaris.

10061 is WSAECONNREFUSED in winerror.h - it's the Windows Socket API's version of ECONNREFUSED.

No doubt on other systems, it's different again.

The correct way to handle this is use symbolic comparisons based on the OS's definition of ECONNREFUSED; that's the way you do it in C, for example. In other words, have a constant called ECONNREFUSED that has the value of ECONNREFUSED for that platform, in a platform-specific library (which will be necessary to link to the OS's socket primitives in any case), and compare error codes with the ECONNREFUSED constant, rather than magic numbers.

I don't know what Python's standard approach to OS error codes is. I suspect it's not terribly well thought out.

Barry Kelly
It does appear that python has a way to handle OS error codes by using the errno package. It contains a symbol for each standard error. Including the one in the question.And it's available as back as the 2.4.1 at least.
Avihu Turzion
If I'm not mistaken, 10061 is a *TCP* error code (it also shows on MVS for example). So if the OS chooses to translate it I'm not sure you can "blame" Python for that.
Albert Visser
There you go - you should probably patch the library to use those error code symbolic values (don't add other magic numbers, as magic numbers from different platforms may clash with one another) - and notify the author of the library of the defect.
Barry Kelly
avisser - 10061 really is the value of WSAECONNREFUSED on Windows (look in winerror.h), and is only TCP-specific in so far as the vast majority of connection-oriented protocols using sockets these days are TCP-based.
Barry Kelly