Is socket error handling different in python 2.5 than 2.7?

Below is a snapshot of the error tracing code for a Windows service running python. I am running. It seems to work fine in python 2.7 on windows xp, but python 2.5 runs on a Windows 2003 server on the working computer that I am using. The main error I encountered is the 'error' object has no attribute 'errno' Am I doing what is fundamentally wrong for python 2.5, which works for 2.7?

Code abbreviation:

 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 tracking error:

 if socket_error.errno == errno.ECONNREFUSED: AttributeError: 'error' object has no attribute 'errno' %2: %3 
+4
source share
1 answer

As explained here in python 2.6:

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

and the child classes IOError have the errno member.

If you want to get errno associated with your error in python <2.6, (and assuming the method described here is valid for raising socket.error), you will have to get it from the args attribute of your exception:

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

Source: https://habr.com/ru/post/1383426/


All Articles