Sub topic 6
HTTP on ports 80 and 443
This chapter assumes you know what a port is, how a socket is formed from an IP address, a port and a transport protocol, and that you can read short Python scripts and run terminal commands.
The two web ports
HTTP (Hypertext Transfer Protocol) is the request and response protocol that web browsers and servers use. It runs on TCP, and it has two standard ports:
| Port | Protocol | Notes |
|---|---|---|
| 80 | HTTP | Plain text. Anyone on the network path can read or change the traffic. |
| 443 | HTTPS | HTTP carried inside a TLS (Transport Layer Security) session. |
Browsers pick the port from the URL scheme. http://example.com/ means port 80, and https://example.com/ means port 443. You only see a port in a URL when it differs from the default, as in http://example.com:8080/.
HTTP is just text on a port
On port 80, an HTTP/1.1 request is lines of ASCII text ending in a carriage return and line feed (\r\n). A blank line ends the headers. This script sends a HEAD request, which asks for headers only:
import socket
request = (
"HEAD / HTTP/1.1\r\n"
"Host: example.com\r\n"
"Connection: close\r\n\r\n"
).encode("ascii")
with socket.create_connection(("example.com", 80), timeout=5) as s:
s.sendall(request)
print(s.recv(1024).decode("latin-1"))The output starts with a status line such as HTTP/1.1 200 OK, followed by headers. The port carries no meaning beyond "connect here". The agreed-upon bytes are what make it HTTP.
The Host header matters because many sites share one IP address and one port. The server uses Host to decide which site to serve.
How a connection looks
Why HTTPS uses a separate port
With HTTPS, the TCP connection to port 443 first performs a TLS handshake. Only after that do the HTTP bytes flow, now encrypted. The server proves its identity with a certificate, so the client knows it is talking to the right host. The HTTP messages themselves are unchanged. TLS wraps them.
Many sites answer on port 80 only with a redirect (status 301 or 308) to the https:// address. Browsers may also remember that a site wants HTTPS, using the Strict-Transport-Security header, and skip port 80 on later visits.
Common errors
- Sending plain HTTP to port 443. The server expects a TLS handshake and usually replies with an error or closes the connection. The reverse also fails: a TLS handshake sent to port 80 is not valid HTTP.
- Forgetting the port in a URL. Services on ports like 8080 or 8443 need it written out.
- Opening only port 443 on a firewall. Users who type a bare hostname often start on port 80, so a redirect there needs port 80 open.
- Assuming a port implies a protocol. Port 80 can run something other than HTTP. Later chapters cover this.
Exercise
A user reports that https://intranet.example times out, but http://intranet.example redirects correctly. Which port is probably blocked or not listening, and which command from this tutorial would you run on the server to check?
Show answer
Port 443 is probably blocked or has no listener, since port 80 works. Run ss -tulnp on the server to see whether anything is listening on 443. From another machine you own, nc -vz intranet.example 443 tests whether the port accepts connections.