Richard
Richard

Reputation: 15892

Is socket error handling different in python 2.5 than 2.7?

Below is a code snip with error trace of a python scripted windows service I am running. It seems to work fine in python 2.7 on windows xp but the production machine I am using runs python 2.5 on windows server 2003. The main error I am having is 'error' object has no attribute 'errno' Am I doing something that is fundamentally wrong for python 2.5 that works for 2.7?

Code Snip:

try:
     if s == None:
          s = self.connect()
     char = s.recv(1)
     line += char

except socket.timeout:
    if s != None:
        s.close()
    s = None
    continue

except socket.error, socket_error:
    servicemanager.LogErrorMsg(traceback.format_exc())

    if socket_error.errno == errno.ECONNREFUSED:
        if s != None:
            s.close()
        time.sleep(60)

        s =None                    
        continue

    else:
        if s != None:
            s.close()
        os._exit(-1)

else:
    pass

Error Trace Snip:

if socket_error.errno == errno.ECONNREFUSED:
AttributeError: 'error' object has no attribute 'errno' 
%2: %3

Upvotes: 4

Views: 1339

Answers (1)

Cédric Julien
Cédric Julien

Reputation: 80821

As explained here, in python 2.6 :

Changed in version 2.6: socket.error is now a child class of IOError.

and the children classes of IOError have a errno member.

If you want to get the errno related to your error in python < 2.6, (and assuming that the way described here is valid for raising socket.error) you'll have to get it from the args attribute of your exception :

except socket.error, socket_error:
    if socket_error.args[0] == errno.ECONNREFUSED:

Upvotes: 5

Related Questions