Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a website's IP address using Python 3.x?

Tags:

python

ip

dns

I have a string representing a domain name. How can I get the corresponding IP address using Python 3.x? Something like this:

>>> get_ip('http://www.stackoverflow.com')
'64.34.119.12'
like image 218
snakile Avatar asked Jan 27 '11 10:01

snakile


People also ask

How do I get the IP address of a website in Python?

Python get IP Address from hostnamePython gethostbyname() function accept the hostname as an argument and it will return the IP address of some of the website by using the socket module.

How do I get a website IP address?

The simplest way to determine the IP address of a website is to use our DNS Lookup Tool. Simply go to the DNS Lookup Tool, type the website URL into the text entry, and select Lookup. You'll notice the search yielded a list of IPv4 addresses that differ from the IPs shown using the other methods.


1 Answers

>>> import socket

>>> def get_ips_for_host(host):
        try:
            ips = socket.gethostbyname_ex(host)
        except socket.gaierror:
            ips=[]
        return ips

>>> ips = get_ips_for_host('www.google.com')
>>> print(repr(ips))
('www.l.google.com', [], ['74.125.77.104', '74.125.77.147', '74.125.77.99'])
like image 164
Shoshan Avatar answered Sep 28 '22 03:09

Shoshan