I am trying to get IP address of a domain.. i am using following code
>> import socket
>> socket.gethostbyname('www.google.com')
its giving me following error..
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
socket.gethostbyname('www.google.com')
gaierror: [Errno 11001] getaddrinfo failed
what is wrong with my code...is there any other way to get ip address by domain name in python..??? please help...
The domain name system maps the name people use to locate a website to the IP address that a computer uses to locate that website. For example, if someone types "example.com" into a web browser, a server behind the scenes maps that name to the corresponding IP address.
Your code is correct. Perhaps you have a firewall in between you and these servers that is blocking the request?
import socket
domainName = input('Enter the domain name: ')
print(socket.gethostbyname(domainName))
I think you forgot to print it because it works for me.
# Python3 code to display hostname and
# IP address
# Importing socket library
import socket
# Function to display hostname and
# IP address
def get_Host_name_IP():
try:
host_name = socket.gethostname()
host_ip = socket.gethostbyname(host_name)
print("Hostname : ",host_name)
print("IP : ",host_ip)
except:
print("Unable to get Hostname and IP")
# Driver code
get_Host_name_IP() #Function call
#This code is conributed by "Sharad_Bhardwaj".
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