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'
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.
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.
>>> 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'])
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