Originally published on Medium under the Level Up coding publication.
Every time you type google.com
into your browser’s address bar and press Enter, a complex sequence of events unfolds behind the scenes. This post will break down each step in detail with Python examples and references to official documentation.
1. Domain Name Resolution (DNS Lookup)
Before your browser can connect to Google’s servers, it needs the IP address corresponding to google.com
. This process is called DNS resolution.
How it Works:
- The browser first checks its DNS cache to see if the domain has been resolved recently.
- If not found, it queries the OS’s DNS resolver.
- If still unresolved, the OS sends a request to a recursive DNS server (provided by your ISP or a third-party like Google’s
8.8.8.8
). - The recursive DNS server queries root name servers → TLD name servers (
.com
) → Authoritative name servers forgoogle.com
. - The authoritative name server provides the IP address (e.g.,
142.250.184.14
). - The response is cached for future queries.
🔗 Official Docs:
Python Code for DNS Resolution:
import socket
domain = "google.com"
ip = socket.gethostbyname(domain)
print(f"Resolved {domain} to {ip}")
2. Establishing a TCP Connection
Once the IP is known, the browser establishes a TCP connection using the three-way handshake:
- SYN: The client sends a TCP packet with the SYN flag set.
- SYN-ACK: The server responds with a packet containing SYN and ACK flags.
- ACK: The client acknowledges and the connection is established.
🔗 Official Docs:
Python Code for TCP Connection:
import ssl, socket
context = ssl.create_default_context()
sock = socket.create_connection((ip, 443)) # HTTPS uses port 443
secure_sock = context.wrap_socket(sock, server_hostname=domain)
print("TCP connection established with Google")
3. TLS Handshake & Secure Connection
Since Google uses HTTPS, an additional TLS handshake takes place:
- Client Hello: The browser sends supported encryption protocols.
- Server Hello: The server responds with a chosen encryption algorithm.
- Certificate Exchange: The server provides an SSL certificate for verification.
- Key Exchange: Both parties establish an encrypted communication channel.
🔗 Official Docs:
Python Code for TLS Handshake:
print(secure_sock.version()) # Print the TLS version used
4. Sending an HTTP Request
Once connected, the browser sends an HTTP request. A standard request for Google’s homepage:
GET / HTTP/1.1
Host: google.com
Connection: close
User-Agent: Mozilla/5.0 (compatible; Chrome)
🔗 Official Docs:
Python Code for Sending an HTTP Request:
request = "GET / HTTP/1.1\r\nHost: google.com\r\nConnection: close\r\n\r\n"
secure_sock.sendall(request.encode())
5. Receiving and Processing the Response
Google’s server responds with an HTTP response, typically a 301 Redirect:
HTTP/1.1 301 Moved Permanently
Location: https://www.google.com/
This instructs the browser to retry the request at https://www.google.com/
.
🔗 Official Docs:
Python Code for Receiving Response:
response = b""
while True:
data = secure_sock.recv(4096)
if not data:
break
response += data
print(response.decode(errors='ignore'))
6. Rendering the Webpage
Once the browser receives the response, it:
- Parses the HTML.
- Fetches additional resources (CSS, JavaScript, images).
- Executes scripts.
- Renders the page on the screen.
🔗 Official Docs:
7. Handling Subsequent Requests (Cookies, Caching, CDN)
- Cookies: The server might set authentication cookies.
- Caching: The browser stores assets for faster loading in future requests.
- CDN Requests: Additional requests might be sent to Google’s CDN for assets.
🔗 Official Docs:
Conclusion
When you type google.com
and press Enter, a series of sophisticated network processes occur, including DNS resolution, TCP/TLS handshakes, HTTP requests, and webpage rendering. Each step plays a critical role in delivering web pages securely and efficiently.
Want to Try It Yourself?
Run the Python snippets above to see the process in action! 🚀
Top comments (3)
pretty cool actually diving into all this stuff helps me not take the web for granted - ever wonder if breaking things down to basics makes it easier to fix stuff when it breaks?
When I type 'google.com' something went terribly wrong, since I'm a kagi user. 😅
Super clear steps!