DNS and address lookup in Python
Resolving names, ports, and addresses with socket helpers.
import socket
my_name = socket.gethostname()
addresses = socket.getaddrinfo("example.com", 443,
proto=socket.IPPROTO_TCP)
first_ip = addresses[0][4][0]
https_port = socket.getservbyname("https")
ssh_port = socket.getservbyname("ssh")
packed = socket.inet_aton("10.0.0.7")
readable = socket.inet_ntoa(packed)
How it works
getaddrinforesolves a host to IP addresses.getservbynamemaps a service to its port.inet_aton/inet_ntoaconvert address formats.
The run, in numbers
- Lines
- 12
- Characters to type
- 310
- Tokens
- 77
- Three-star pace
- 105 tpm
At the three-star pace of 105 tokens a minute, this run takes about 44 seconds.
Step 4 of 4 in Sockets & networking, step 9 of 41 in Domain tools.