DEV Community

Cover image for What Really Happens When You Type `google.com` in Your Browser?
Kfir
Kfir

Posted on

What Really Happens When You Type `google.com` in Your Browser?

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:

  1. The browser first checks its DNS cache to see if the domain has been resolved recently.
  2. If not found, it queries the OS’s DNS resolver.
  3. 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).
  4. The recursive DNS server queries root name serversTLD name servers (.com) → Authoritative name servers for google.com.
  5. The authoritative name server provides the IP address (e.g., 142.250.184.14).
  6. 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}")
Enter fullscreen mode Exit fullscreen mode

2. Establishing a TCP Connection

Once the IP is known, the browser establishes a TCP connection using the three-way handshake:

  1. SYN: The client sends a TCP packet with the SYN flag set.
  2. SYN-ACK: The server responds with a packet containing SYN and ACK flags.
  3. 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")
Enter fullscreen mode Exit fullscreen mode

3. TLS Handshake & Secure Connection

Since Google uses HTTPS, an additional TLS handshake takes place:

  1. Client Hello: The browser sends supported encryption protocols.
  2. Server Hello: The server responds with a chosen encryption algorithm.
  3. Certificate Exchange: The server provides an SSL certificate for verification.
  4. 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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

🔗 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())
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

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'))
Enter fullscreen mode Exit fullscreen mode

6. Rendering the Webpage

Once the browser receives the response, it:

  1. Parses the HTML.
  2. Fetches additional resources (CSS, JavaScript, images).
  3. Executes scripts.
  4. 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)

Collapse
 
nevodavid profile image
Nevo David

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?

Collapse
 
syeo66 profile image
Red Ochsenbein (he/him)

When I type 'google.com' something went terribly wrong, since I'm a kagi user. 😅

Collapse
 
dotallio profile image
Dotallio

Super clear steps!