Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IP address by Domain Name

Tags:

python

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

like image 222
Rajesh Avatar asked Jun 21 '11 09:06

Rajesh


People also ask

Does the domain name identify the IP address?

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.


3 Answers

Your code is correct. Perhaps you have a firewall in between you and these servers that is blocking the request?

like image 82
abhiasawa Avatar answered Sep 28 '22 05:09

abhiasawa


import socket
domainName = input('Enter the domain name: ')
print(socket.gethostbyname(domainName))

I think you forgot to print it because it works for me.

like image 39
MartinCo Avatar answered Sep 28 '22 05:09

MartinCo


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

like image 25
uTesla Avatar answered Sep 28 '22 04:09

uTesla