2.5. Ports
Table of Contents
Understanding Ports
When your browser talks to a server on the Internet, it is not enough to know which computer to talk to. You also need to know which application on that computer should receive the data. This is what ports are for.
This chapter explains ports from a backend developer perspective, with many practical examples you will see in web development.
What Is a Port?
Every machine on a network is identified by an IP address. But a machine almost always runs many networked programs at the same time. For example, on one server you might have:
- A web server for your website
- A database server
- An SSH server for remote login
- A Redis server for caching
- A mail server
All of these need to receive and send data over the network.
A port is a number that identifies a specific networked application (or service) on a machine.
You can think of it like this:
- IP address: the building address
- Port: the apartment number inside that building
To talk to a specific service, you need both:
- IP address, for example
203.0.113.10 - Port number, for example
5432
Together they give you a complete destination like:
203.0.113.10:5432
This says: "Connect to the machine at IP 203.0.113.10, application listening on port 5432."
Port Numbers and Their Ranges
Ports are just integers between 0 and 65535.
The range is:
$$ 0 \leq \text{port} \leq 65535 $$
This is because ports are stored as 16-bit unsigned numbers, and $2^{16} = 65536$ possible values, from 0 to 65535.
Rule: Valid TCP or UDP port numbers are from 0 to 65535. Values outside this range are invalid.
Port numbers are divided into three main ranges:
| Range | Name | Typical Use |
|---|---|---|
| 0 to 1023 | Well-known ports | Standard services like HTTP, HTTPS, SSH, FTP, DNS, etc. |
| 1024 to 49151 | Registered ports | Services registered by companies or projects |
| 49152 to 65535 | Dynamic / private | Temporary ports chosen by clients (ephemeral ports) |
As a backend developer, you will most often care about:
- Some well-known ports for standard protocols
- A few custom ports for your own development servers
Common Ports Backend Developers Should Know
Here are some of the ports you will see all the time.
Web and API related
| Port | Protocol / Use | Example |
|---|---|---|
| 80 | HTTP | http://example.com defaults to port 80 |
| 443 | HTTPS | https://example.com defaults to port 443 |
| 8080 | Alternative HTTP port | Local dev servers, proxies, test servers |
| 3000 | Common dev port | Node.js, frontend dev servers, some APIs |
| 8000 | Common dev port | Django, FastAPI, local API servers |
| 8001 | Alt. dev / admin port | Admin interfaces, second service instance |
Database related
| Port | Service (often default) |
|---|---|
| 5432 | PostgreSQL |
| 3306 | MySQL / MariaDB |
| 6379 | Redis |
| 27017 | MongoDB |
System and remote access
| Port | Service |
|---|---|
| 22 | SSH |
| 25 | SMTP (sending email) |
| 53 | DNS |
You do not have to memorize all ports, but you should quickly recognize:
- 80 and 443, for HTTP and HTTPS in production
- 5432, for PostgreSQL
- 6379, for Redis
- 22, for SSH
How Ports Work with IP and Protocol
A full network address for a service is actually a combination of:
- Protocol, for example TCP or UDP
- IP address
- Port number
So a connection is identified by:
$$ \text{Protocol} + \text{IP} + \text{Port} $$
As a result, these are all different "endpoints":
TCP 203.0.113.10:5432UDP 203.0.113.10:5432TCP 203.0.113.10:80TCP 198.51.100.7:80
Even if the port numbers match, different protocols or IPs give you different endpoints.
For web backends, you will almost always work with TCP ports. UDP is used in other contexts, such as DNS or streaming.
Important: When people say "port 80" in web development, they almost always mean TCP port 80.
Listening Ports and Active Connections
On a machine, there are two important concepts:
- Listening port
- A server program calls a system function that says: "I want to listen on port X."
- Example: Nginx listens on port 80, PostgreSQL listens on 5432.
- Active connection
- When a client connects, the operating system creates a connection between:
- Client IP + client port
- Server IP + server port
So for one connection you can think of this 4-part combination:
- Client IP
- Client port
- Server IP
- Server port
This combination is often called a "4-tuple."
Clients usually use ephemeral ports (high ports) chosen automatically by the OS.
Example:
- Client:
198.51.100.10:51123 - Server:
203.0.113.10:443
Your browser might be using port 51123 on your machine to connect to port 443 on the server. If you opened another tab to the same site, your browser might open a second connection like:
198.51.100.10:51124to203.0.113.10:443
The server still listens on 443, but there are multiple connections to it from different client ports.
Ports in URLs
When you type a URL in your browser, you often do not see the port. The browser uses default ports based on the scheme:
http://example.commeans HTTP over port 80https://example.commeans HTTPS over port 443
You only see a port in the URL if it is not the default:
http://localhost:8000uses HTTP on port 8000https://api.example.com:8443/v1/usersuses HTTPS on port 8443
Example comparisons:
| URL | Scheme | Host | Port used |
|---|---|---|---|
http://example.com | http | example.com | 80 |
https://example.com | https | example.com | 443 |
http://localhost:3000 | http | localhost | 3000 |
https://api.example.com:8443 | https | api.example.com | 8443 |
As a backend developer, in development you will very often specify ports explicitly in URLs, for example:
- Frontend dev server:
http://localhost:3000 - API dev server:
http://localhost:8000 - Admin dev panel:
http://localhost:8001
Localhost and Common Development Ports
When you run services on your own machine, you almost always connect to localhost with a port:
localhostusually refers to IP127.0.0.1(IPv4) or::1(IPv6).
Examples in backend development:
- Run a FastAPI app: server listens on
127.0.0.1:8000 - Run PostgreSQL locally: listens on
127.0.0.1:5432 - Run Redis locally: listens on
127.0.0.1:6379
Example workflow:
- Start a FastAPI app:
uvicorn main:app --reload --port 8000This command tells Uvicorn to listen on port 8000.
- Now you can open:
http://localhost:8000/in your browser- Or call it from another script:
import requests
response = requests.get("http://localhost:8000/health")
print(response.status_code, response.text)
If you change --port 8000 to --port 5000, then you must also change the URLs you use to http://localhost:5000.
What Happens If a Port Is Already in Use?
Only one process on a machine can listen on a specific port for a specific protocol and IP combination.
If you try to start a second server on the same port, you will usually get an error, for example in Python:
OSError: [Errno 98] Address already in useTypical situations:
- You already have a server running on port 8000.
- You try to run another development server on the same port.
- The OS refuses the second one.
Solutions:
- Stop the existing server.
- Or use a different port, for example 8001 instead of 8000.
As a backend developer, you will often change ports when:
- Running multiple apps locally at the same time.
- Running multiple versions of the same app, for example production and staging.
Ports and Firewalls
A firewall is a system that can allow or block network traffic based on rules. One of the most common things a firewall checks is the port number.
For example, on a production server you might configure:
- Allow incoming TCP connections on ports:
- 22 (SSH)
- 80 (HTTP)
- 443 (HTTPS)
- Block all other incoming ports
This means:
- If your API tries to listen on port 8000 and the firewall blocks it, clients on the Internet will not be able to connect, even if the server is running.
- Locally, on your machine, you can still access
http://localhost:8000because the firewall usually does not block loopback (localhost) traffic by default.
In cloud providers like AWS, DigitalOcean, GCP, you will see firewall-like settings called "security groups" or "firewalls" that typically specify allowed port ranges.
Ports on the Server vs Ports on the Client
When a client connects to a server, both sides use ports, but for different purposes.
- Server port
- Fixed, known and usually standard (like 80, 443, 5432).
- The server listens on this port and waits for connections.
- Client port
- Chosen automatically from the ephemeral port range.
- Only used for that one connection.
- The client usually does not care about the exact number.
For example, when your browser connects to a website:
- Server is listening on
203.0.113.10:443 - Your machine might use
192.0.2.20:51234as client port.
If you click another link:
- Browser might open another connection:
192.0.2.20:51235to203.0.113.10:443
You usually only configure the server side ports. The client side ports are handled by the operating system.
Multiple Services on One Server With Different Ports
One physical or virtual machine can host many services at once by using different ports.
Example:
| Service | Port | URL or Address |
|---|---|---|
| Main website | 80 | http://example.com |
| Secure website | 443 | https://example.com |
| Admin API | 8443 | https://example.com:8443 |
| PostgreSQL DB | 5432 | postgres://db.example.com:5432/... |
| Redis cache | 6379 | Connected to from app code, not browser |
Clients choose which service to talk to by picking the right port.
In many real deployments, you might hide some ports behind a reverse proxy so that external clients only see port 80 and 443, while your backend services talk to each other on internal ports.
Ports and Docker (Briefly)
You will learn Docker in detail later, but ports are important there too.
When you run a Docker container, it can expose a port inside the container. To reach it from your host machine, you map a host port to a container port.
Example:
docker run -p 8000:80 my-web-appThis means:
- Container listens on its internal port 80.
- Docker maps host port 8000 to container port 80.
- You can now open
http://localhost:8000, which forwards to port 80 inside the container.
You will do this often when running databases, Redis, or API servers in containers.
Practical Exercises You Can Try
You can try these on a Linux or macOS terminal. On Windows, use PowerShell with appropriate equivalents.
See which ports are listening
On Linux or macOS:
sudo lsof -i -P -n | grep LISTENYou will see output like:
nginx 1234 root 6u IPv4 12345 0t0 TCP *:80 (LISTEN)
postgres 5678 postgres 7u IPv4 23456 0t0 TCP 127.0.0.1:5432 (LISTEN)This tells you which applications are listening on which ports.
Run a simple Python HTTP server on a custom port
In a directory with some files, run:
python -m http.server 9000Now open:
http://localhost:9000/
If you run a second one on a different port:
python -m http.server 9001You can now access:
http://localhost:9000/http://localhost:9001/
Same program, same machine, different ports, so they can coexist.
Summary
- A port is a number that identifies a specific networked service on a machine.
- Valid ports are from 0 to 65535.
- Common web ports:
- 80 for HTTP
- 443 for HTTPS
- Many developer tools use ports like 3000, 8000, 8080 for local development.
- A connection is defined by protocol, IP addresses, and ports on both sides.
- Only one process can listen on a given IP + protocol + port combination.
- Firewalls often control access based on ports.
- In URLs, ports are usually hidden when you use the default (80 for HTTP, 443 for HTTPS), and shown explicitly when you use a custom port.
Understanding ports is essential when you configure servers, connect to databases, run services in Docker, or debug network connectivity issues in backend development.
Views: 9
KAHIBARO