Something Crazy about localhost: Unveiling the Inner Workings
Tags: webdev, programming, python
Published: 2024-06-18 05:36:20

Ever wonder when you type `localhost` into your browser, you might take for granted that it magically knows to connect your local computer. However, there's a fascinating mechanism behind how `localhost` works. 

There were a bunch of questions I had in my mind. 
1. How does it map to an IP address?  
2. Does it function similarly to DNS? 


## Let's dive into the details of localhost
**Localhost** is a hostname that refers to the current device used to access it. The name resolves to the IP address by default as `127.0.0.1` for IPv4 and `::1` for IPv6, pointing to the loopback network interface. 

Wait what's a loopback Network Interface?

The loopback interface is a kind of special network interface used by the local computer to send network traffic to itself. Imagine it's like a virtual network device that software on your computer uses to test network applications without needing access to a physical network

- IPv4 -> `127.0.0.1`
- IPv6 -> `::1`

That's great, So how does the loopback interface know this information ?

## Mapping of Localhost to IP

The Mapping varies based on the operating system.

- Mac / Linux System -> `/etc/hosts`
- Windows: `C:\Windows\System32\drivers\etc\hosts`

Now you can play around with this hosts file and see how it's mapping magically. 

## Does Localhost Work Like DNS?
While both localhost and DNS (Domain Name System) serve the purpose of resolving hostnames to IP addresses, they operate quite differently.

Hosts File: The resolution of localhost is handled locally through the hosts file. When you access localhost, the operating system reads the hosts file and maps localhost to 127.0.0.1 or ::1.

DNS: DNS is a hierarchical and decentralized naming system used for resolving domain names to IP addresses on the Internet. When you type a domain name (e.g., example.com) into your browser, the DNS resolver contacts multiple DNS servers to find the corresponding IP address.


## Playing around with localhost using Python

```python
import socket
print(socket.getaddrinfo('localhost', 0))
```

output:
```
[(<AddressFamily.AF_INET6: 30>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('::1', 0, 0, 0)), (<AddressFamily.AF_INET6: 30>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('::1', 0, 0, 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('127.0.0.1', 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('127.0.0.1', 0))]
```

Noticed the localhost carries both IPv4 and IPv6. So by using the IP address directly would be more efficient which would reduce the lookup. Interesting. 


## Conclusion
The simplicity and reliability of localhost mask the sophisticated mechanisms working behind the scenes. By mapping to 127.0.0.1 or ::1 through the hosts file, localhost provides an essential tool for developers and system administrators.

## What did we learn
1. IP address directly would be more efficient which would reduce the lookup
2. IP addresses are just present within a hosts file.
3. DNS and local mapping of IP's are different


Thanks for reading my first blog about something we use daily.