typestar

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

  1. getaddrinfo resolves a host to IP addresses.
  2. getservbyname maps a service to its port.
  3. inet_aton/inet_ntoa convert 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.

Type this snippet

Step 4 of 4 in Sockets & networking, step 9 of 41 in Domain tools.

← Previous Next →