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'
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'
Note that:
If these are problematic, use socket.getaddrinfo() instead.
The easiest way is to use
socket.gethostbyname()
. This does not support IPv6, though, and is based on the deprecated C callgethostbanme()
. If you care about these problems, you can use the more versatilesocket.getaddrinfo()
instead.