2

I have some python code that binds to a socket. I catch any exception so that I can handle it. Depending on the error, I want to do different things. How do I know what an error number means?

For example, I want to do something like:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
   s.bind(('', 80))
except socket.error, ex
   (error_number, error_message) = ex
   if error_number==EADDRINUSE:
      print "You can't bind to that port -- someone else is using it"
   else:
      print "Unknown socket error"

except that I don't know what error number corresponds to EADDRINUSE. On my Ubuntu box it is 98, on a Mac running OS X 10.5 it is 48, and on a Windows XP box it is 10048. Is there some place where these are included in a python library?

I realize that the numbers are different because of differences in errno.h in the C layer, but I do not know where they are exposed in python. Similarly, I assume that I can't rely on the error message being a specific string.

Also, I'm not just concerned about EADDRINUSE -- I'd like to cover other errors, too.

1 Answer 1

2

These errors are all defined in the errno module in the standard library.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.