1.TCP/IP, IP addresses, and ports
TCP/IP is the layered set of protocols the internet runs on: IP delivers a packet to the right HOST using an IP address, and a port number then delivers it to the right PROCESS on that host. TCP is the layer on top that makes delivery reliable and ordered.
Key features
- An IPv4 address is 32 bits, written as four dotted decimal octets (192.168.1.10); IPv6 is 128 bits, written in colon-separated hex groups
- A port is a 16-bit number (0-65535) identifying a specific process on a host — 0-1023 are well-known ports (80 HTTP, 443 HTTPS, 22 SSH), assigned by IANA
- A TCP/UDP connection endpoint is the PAIR (IP address, port) — the same host can run many independent services, one per port
- The internet protocol stack layers roughly: Link (Ethernet/Wi-Fi) -> Internet (IP) -> Transport (TCP/UDP) -> Application (HTTP, DNS, ...), each layer only trusting the guarantees of the one below it
- IP itself is unreliable and connectionless — it does not guarantee a packet arrives, arrives once, or arrives in order; TCP is the layer that adds those guarantees on top
The four layers a web request actually crosses
- Application data — an HTTP request, a DNS query, ...
- TCP/UDP + port — e.g. destination port 443 — picks the process
- IP + IP address — e.g. destination 192.168.1.10 — picks the host
- Link layer — Ethernet/Wi-Fi frame — picks the device on this wire
Command
import ipaddress
ip = ipaddress.ip_address('192.168.1.10') # ValueError if not a valid IPv4/IPv6 address
net = ipaddress.ip_network('192.168.1.0/24') # a whole subnet
port = 443 # just an int in [0, 65535] -- no dedicated typeAn IP address routes to a HOST; a port routes to a PROCESS on that host; a service is really identified by the (IP, port) pair together, not by either alone.