views:

50

answers:

1

Is it possible to set a timeout on a getaddrinfo() call in CPython 2.7?

socket.setdefaulttimeout() does not work. I don't really want a solution that wraps a function using threads or signals. A solution that only uses the standard library is best, but using a third-party package would be acceptable.

For example, I want to do this:

socket.getaddrinfo("""!@#$%^&*()+=-[]\\\';,./{}|\":<>?~_""", None)

And have it raise a socket.error in 1 second. (Note that when I run this on OS X it times out rapidly anyway, but running on Debian it takes about 60 seconds to fail).

+2  A: 

My understanding is that getaddrinfo is wrapper over OS provided library:

On unix:

int getaddrinfo(const char *nodename, const char *servname,
                const struct addrinfo *hints, struct addrinfo **res);

On Windows:

int WSAAPI getaddrinfo(
  __in_opt  PCSTR pNodeName,
  __in_opt  PCSTR pServiceName,
  __in_opt  const ADDRINFOA *pHints,
  __out     PADDRINFOA *ppResult
);

None of them uses has a timeout value and hence Python can not provide you directly. You have to either code that using threads or use a third party library.

pyfunc
Or use a signal.
Thomas Wouters