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?
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.
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With