saz
saz

Reputation: 995

is there any timeout value for socket.gethostbyname(hostname) in python?

I'm translating hostname to IPv4 address using gethostbyname() of socket in python. Sometimes it takes little extra time to show the IP address. I was wondering if there is any default timeout value for each lookup. Here's how i'm using socket in my program-

try:
    addr = socket.gethostbyname(hostname)
except socket.gaierror:
    addr = "" 
print hostname+" : "+addr

Just need to add another question, is there any chance that this can miss any IP address? Anyone had any experience converting large sample of hostname to IP address?

Upvotes: 15

Views: 15396

Answers (3)

GuruOfAll007
GuruOfAll007

Reputation: 37

A simple solution would be:

import socket
socket.setdefaulttimeout(5) #Default this is 30
socket.gethostbyname(your_url) 

Upvotes: -1

Bryce Guinta
Bryce Guinta

Reputation: 3702

Here's an example of using the alarm signal (for posix type systems) to set a timeout for socket.gethostbyname:

Setup code:

from contextlib import contextmanager
import signal

def raise_error(signum, frame):
    """This handler will raise an error inside gethostbyname"""
    raise OSError

@contextmanager
def set_signal(signum, handler):
    """Temporarily set signal"""
    old_handler = signal.getsignal(signum)
    signal.signal(signum, handler)
    try:
        yield
    finally:
        signal.signal(signum, old_handler)

@contextmanager
def set_alarm(time):
    """Temporarily set alarm"""
    signal.setitimer(signal.ITIMER_REAL, time)
    try:
        yield
    finally:
        signal.setitimer(signal.ITIMER_REAL, 0) # Disable alarm

@contextmanager
def raise_on_timeout(time):
    """This context manager will raise an OSError unless
    The with scope is exited in time."""
    with set_signal(signal.SIGALRM, raise_error):
        with set_alarm(time): 
            yield

Execution code

import socket
try:
    with raise_on_timeout(0.1): # Timeout in 100 milliseconds
        print(socket.gethostbyname(socket.gethostname()))
except OSError:
    print("Could not gethostbyname in time")

Upvotes: 4

David Greydanus
David Greydanus

Reputation: 2567

Here is the entire Socket time out file.

import socket

try:
    _GLOBAL_DEFAULT_TIMEOUT = socket._GLOBAL_DEFAULT_TIMEOUT
except AttributeError:
    _GLOBAL_DEFAULT_TIMEOUT = object()

As you can see, GLOBAL_DEFAULT_TIMEOUT = object() is a just creating an empty object.

socket.setsocketimeout will set the default timeout for new sockets, however if you're not using the sockets directly, the setting can be easily overwritten.

For more, see this answer.

EDIT: As for your follow up question, yes. I made a program that involved host name to IP address conversion, and I did have issues with it missing addresses. Not sure if this was because of a timeout. I just had to double check.

Upvotes: 2

Related Questions