Looking up the IP behind a domain sounds like a one-liner, and it mostly is. But if you have ever compared results between your laptop, a server and an online tool and got three different answers, it helps to know what is going on. Here is a short tour.
Python
The quickest option uses the standard library:
import socket
print ( socket . gethostbyname ( "example.com" ) ) # first IPv4 address only
Enter fullscreen mode
Exit fullscreen mode
gethostbyname returns a single IPv4 address. To see everything the resolver returns, including IPv6:
import socket
infos = socket . getaddrinfo ( "example.com" , 443 , proto = socket . IPPROTO_TCP ) for family , _ , _ , _ , sockaddr in infos : print ( family . name , sockaddr [ 0 ] )
Enter fullscreen mode
Exit fullscreen mode
Node.js
Node has two different APIs, and the difference matters:
const dns = require ( 'node:dns' ) . promises ;
// Uses the OS resolver (same as most apps, respects /etc/hosts) const { address } = await dns .…