Skip to main content
In this lesson, we explore the fundamentals of IP addresses and ports from a web application perspective. Beginners frequently encounter connectivity issues, such as: • Which IP addresses should be used and what do port numbers represent?
• What distinguishes the localhost (127.0.0.1) from a server’s public IP address?
• Why might a system fail to reach a web server running on another host?
• When both a web server and a database server are deployed, why might communication between them fail?
This guide demystifies these concepts so you can troubleshoot and resolve network issues with confidence.

Network Interfaces and IP Addresses

Every computer—whether it’s a laptop or a server—features one or more network interfaces (adapters). Examples include wired Ethernet and wireless (Wi-Fi) adapters. When your device connects to a network, each interface receives its own IP address. For instance, connecting your laptop to a network switch with an Ethernet cable might assign it an IP address like 10.0.2.15. To view details about your network interfaces, run:
If you later attach a Wi-Fi adapter, it will obtain its own IP address. The output might then resemble:
In this scenario, the laptop has two distinct IP addresses on the same network. Other systems can use either address to reach your device.

Ports and Application Endpoints

Every network interface supports up to 65,535 ports. Think of ports as unique communication endpoints that allow services like web servers to operate concurrently. For example, a Python Flask web server listens on port 5000 by default. You can change the port by specifying it during the application’s runtime. Consider the following simple Flask application:
To configure the application to run on port 8000, pass the port number as a parameter. With multiple IP addresses in your host, you must also decide which interface will serve the web application. For example, bind the server to IP address 10.0.2.15 on port 8000 with:
This setup ensures that only requests sent to 10.0.2.15 on port 8000 are processed. To allow connections via any interface, use 0.0.0.0:
This configuration instructs the server to listen on all network interfaces, enabling it to receive requests regardless of the specific IP used. For development or testing scenarios where access should be limited to your machine, bind the server to the loopback interface (127.0.0.1):
Binding to 127.0.0.1 ensures that your application is secure and only accessible from your own machine during development.

The Loopback Interface and Local Testing

Every host includes a built-in virtual interface known as the loopback interface, which always uses the IP address 127.0.0.1. When you access 127.0.0.1 (or “localhost”), you are communicating with your own system. Importantly, traffic sent to this address does not leave your computer. To check the loopback interface details, run:
During testing, you can simply use “localhost” in your browser rather than the loopback IP address. Keep in mind, however, that the loopback interface is only accessible from within the same host. Attempting to access a server bound to 127.0.0.1 from another device will result in a connection failure. For example, running:
illustrates that a server running on localhost is only reachable from the same machine. Attempts to access it from other devices will not work.
The image illustrates network ports and IP addresses, showing successful and unsuccessful connections to localhost on different devices.
Remember: The loopback interface (127.0.0.1) is strictly for local testing. Accessing it from a different host will lead to connection errors.

That concludes this lesson on IP addresses and ports. Practice using different network configurations to solidify your understanding, and explore related topics in subsequent lessons. Happy coding!

Watch Video

Practice Lab