IP address by Domain Name

Viewed 40143

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...

4 Answers
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". 

Related