Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

sockets

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?

like image 248
saz Avatar asked Dec 05 '14 20:12

saz


People also ask

What is the default timeout of a socket in Python?

New in version 3.3. Return the default timeout in seconds (float) for new socket objects. A value of None indicates that new socket objects have no timeout. When the socket module is first imported, the default is None.

What is Gethostbyname in socket?

DESCRIPTION. Given the name of a host, gethostbyname returns a pointer to the hostent structure containing the host's IP address and other information. Refer to <netdb. h> for details on the hostent structure. This structure is typically used to find the previous address of the host via the h_addr field.

How does Python handle connection timeout exception?

As for the problem with timeout, all you need to do is to change except socket. Timeouterror: to except timeout: , since timeout class is defined inside socket module and you have imported all its members to your namespace.

How do I get the hostname of a socket in Python?

Python code to get a hostname using the socket module Python socket module has a function named gethostname (), using which we can easily find the hostname of a given machine. Syntax – socket.gethostname () The gethostname () doesn’t accept any parameters, but it returns the current hostname of the machine in string format.

What is the return type of gethostbyname ()?

Given a host name the gethostbyname () function returns the IP address of the host. Unlike, gethostbyname () returns just one ip of the host even though the host could resolve to multiple IPs from a given location. The returned IP address is an IPv4 address.

What is the difference between gethostbyname () and given host name?

Given a host name the gethostbyname () function returns the IP address of the host. Unlike, gethostbyname () returns just one ip of the host even though the host could resolve to multiple IPs from a given location.

How to resolve the hostname into IPv6 addresses using socket?

If the developer needs to resolve the hostname into IPv6 addresses or both IPv4 and IPv6 addresses are needed, socket.getaddrinfo () function can be used. When the parameter hostname is omitted hostname defaults to localhost. hostname - The name of the host system for which IP address resolution is needed.


1 Answers

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.

like image 192
David Greydanus Avatar answered Oct 24 '22 17:10

David Greydanus