Menu

Resolving a Domain to Its IP Address in Python and Node.js (and Why Results Differ)
📰

Resolving a Domain to Its IP Address in Python and Node.js (and Why Results Differ)

DEV Community: node·DAPA Checker·7 days ago
#toPFQIel
#devtonodejs#devto#dev#different#socket
Long article (449 words) — no AI summary yet.
Reading 0:00
15s threshold

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

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.