DEV Community

Cover image for Solved: Check Website Response Time from Multiple Regions using Python Requests
Darian Vance
Darian Vance

Posted on • Originally published at wp.me

Solved: Check Website Response Time from Multiple Regions using Python Requests

🚀 Executive Summary

TL;DR: Website performance varies globally, impacting user experience and SEO. This guide provides a Python script using the requests library to measure website response times from multiple geographical regions, enabling identification of bottlenecks and optimization of infrastructure for consistent global performance.

🎯 Key Takeaways

  • The measure\_latency function utilizes time.perf\_counter() for high-resolution timing to accurately calculate response times in milliseconds.
  • Regional context is provided by setting the REGION\_NAME environment variable, allowing the same Python script to report its origin when deployed across different geographical locations.
  • Multi-regional checks are orchestrated by deploying the script on cloud instances (e.g., AWS EC2, Google Cloud VMs) in various regions and scheduling its execution with cron jobs.

Check Website Response Time from Multiple Regions using Python Requests

Introduction

In today’s globally connected digital landscape, the performance of your web applications directly impacts user experience, SEO rankings, and ultimately, your business’s bottom line. A website that loads quickly for users in North America might be painfully slow for users in Asia or Europe, leading to frustration and lost engagement. Understanding website response times from various geographical perspectives is crucial for identifying bottlenecks, optimizing infrastructure, and ensuring a consistent, high-quality experience for all your users.

This tutorial, crafted for SysAdmins, Developers, and DevOps Engineers, will guide you through building a simple yet effective Python script using the popular requests library. This script will measure website response times and, crucially, enable you to contextualize these measurements by running it from different regional deployments. By the end of this guide, you’ll have a robust method for monitoring your website’s global performance.

Prerequisites

Before you begin, ensure you have the following:

  • Python 3.x: Installed on your local machine or target servers. You can download it from the official Python website.
  • requests Library: Python’s elegant and simple HTTP library. Install it using pip:
  pip install requests
Enter fullscreen mode Exit fullscreen mode
  • Basic Python Knowledge: Familiarity with Python syntax, functions, and standard library usage.
  • Access to Remote Servers/VMs: To truly simulate “multiple regions,” you will need access to virtual machines or container instances deployed in different geographical locations (e.g., AWS EC2 instances, Google Cloud VMs, Azure instances, or self-hosted servers).

Step-by-Step Guide

Step 1: Set Up Your Project Directory

Begin by creating a dedicated directory for your project. This helps keep your scripts and any potential configuration files organized.

mkdir website_monitor
cd website_monitor
Enter fullscreen mode Exit fullscreen mode

Step 2: Craft the Core Python Script for Latency Measurement

Create a Python file, for example, check_website_latency.py. This script will contain the logic to measure the response time for a given URL and identify which region the check is originating from.

import requests
import time
import os
import sys

# Define the websites to monitor
TARGET_URLS = [
    "https://www.techresolve.io",
    "https://www.google.com",
    "https://httpbin.org/delay/1" # Example for a delayed response
]

def measure_latency(url):
    """
    Measures the response time for a given URL.
    Returns the latency in milliseconds or an error message.
    """
    try:
        start_time = time.perf_counter()
        response = requests.get(url, timeout=10) # 10-second timeout
        end_time = time.perf_counter()

        latency_ms = (end_time - start_time) * 1000

        # Check for HTTP errors (e.g., 404, 500)
        response.raise_for_status() 

        return f"{latency_ms:.2f} ms"
    except requests.exceptions.Timeout:
        return "Timeout (gt; 10s)"
    except requests.exceptions.ConnectionError:
        return "Connection Error"
    except requests.exceptions.HTTPError as e:
        return f"HTTP Error: {e.response.status_code}"
    except Exception as e:
        return f"An unexpected error occurred: {e}"

if __name__ == "__main__":
    # Get the region name from an environment variable or default to 'Unknown-Region'
    # This allows the same script to report its origin when deployed regionally.
    region_name = os.getenv('REGION_NAME', 'Unknown-Region')

    print(f"--- Website Latency Check from Region: {region_name} ---")

    for url in TARGET_URLS:
        latency = measure_latency(url)
        print(f"URL: {url}, Latency: {latency}")

    print(f"--- Check Complete for Region: {region_name} ---")
Enter fullscreen mode Exit fullscreen mode

Code Logic Explained:

  • The script imports necessary modules: requests for HTTP requests, time for precise timing, and os/sys for environment variable access.
  • TARGET_URLS is a list of the websites you intend to monitor. You should customize this with your own applications.
  • The measure_latency function takes a URL, records the start time, makes an HTTP GET request using requests.get() with a 10-second timeout, and records the end time.
  • time.perf_counter() is used for high-resolution timing, ideal for performance measurements.
  • The response time is calculated and converted to milliseconds.
  • Robust error handling is included to catch common issues like timeouts, connection errors, and HTTP status code errors (e.g., 404, 500), providing clear feedback instead of crashing.
  • In the main execution block (if __name__ == "__main__":), the script retrieves the current region’s name from the REGION_NAME environment variable. If not set, it defaults to ‘Unknown-Region’. This is key for identifying where the check originated.
  • It then iterates through the TARGET_URLS, calls measure_latency for each, and prints the URL and its measured latency, prefixed with the region name.

Step 3: Configure Regional Context for Execution

To differentiate results originating from distinct geographical locations, you’ll pass a unique identifier (the region name) to the script via an environment variable. This allows the same script to be deployed across multiple servers, each identifying its own region.

On your command line, before running the script, set the REGION_NAME environment variable:

# For a server in US-East
REGION_NAME="US-East-1" python3 check_website_latency.py

# For a server in EU-West
REGION_NAME="EU-West-2" python3 check_website_latency.py

# For a server in Asia-Pacific
REGION_NAME="AP-Southeast-1" python3 check_website_latency.py
Enter fullscreen mode Exit fullscreen mode

Each execution will now report its results with the specified region, providing crucial context.

Step 4: Orchestrate Multi-Regional Checks

The true power of this setup comes from deploying and running this script on multiple servers located in different geographical regions. This could involve cloud instances (e.g., EC2, GCP Compute Engine, Azure VMs), Docker containers, or even serverless functions.

Deployment Strategy:

  1. Deploy the Script: Copy check_website_latency.py to each of your monitoring servers in various regions (e.g., /home/user/website_monitor/check_website_latency.py).
  2. Schedule with Cron: Use a cron job on each server to run the script at regular intervals. Open your cron editor and add an entry specific to each region.
# Example cron entry for a server in US-East-1 (runs every 5 minutes)
# Remember to adjust the path to your script
*/5 * * * * REGION_NAME="US-East-1" python3 /home/user/website_monitor/check_website_latency.py

# Example cron entry for a server in EU-West-2
*/5 * * * * REGION_NAME="EU-West-2" python3 /home/user/website_monitor/check_website_latency.py
Enter fullscreen mode Exit fullscreen mode

This setup ensures that your website’s performance is consistently monitored from the user’s perspective in key regions. The output from each cron job can be directed to a log file (e.g., logs/latency_check.log) for later analysis, or sent to a monitoring system.

Common Pitfalls

  • Network Access Restrictions: Your monitoring servers might have outbound firewall rules preventing access to the target websites, leading to “Connection Error” or “Timeout.” Ensure that your servers can reach the internet and specifically the ports your web servers are listening on (typically 80 and 443).
  • Website Blocking/DDoS Protection: Rapid, repeated requests from the same IP address (especially from a single monitoring server) can sometimes trigger WAFs or DDoS protection mechanisms on the target website, leading to temporary IP bans or CAPTCHA challenges. Consider increasing the interval between checks or distributing checks across more IPs if this becomes an issue.
  • Inaccurate Timing Due to Server Load: The accuracy of time.perf_counter() can be subtly affected by high CPU load or I/O contention on the monitoring server itself. While generally highly accurate, ensure your monitoring servers aren’t overloaded if precise millisecond-level accuracy is paramount.
  • DNS Resolution Issues: If your monitoring server has an issue resolving the domain name, you’ll see a connection error. Verify DNS settings on the monitoring server if this occurs.

Conclusion

Monitoring website response times from multiple geographical regions is an indispensable practice for any organization aiming to deliver an optimal user experience globally. With this Python-based solution using the requests library, you now have a foundational tool to gain critical insights into your application’s performance across different parts of the world.

This approach can be further enhanced by integrating the output into centralized logging systems (like ELK stack, Splunk), sending alerts to notification services (Slack, PagerDuty) if thresholds are breached, or visualizing the data with tools like Grafana. Start deploying this script today to ensure your website’s global reach is as performant as it is wide.


Darian Vance

👉 Read the original article on TechResolve.blog


☕ Support my work

If this article helped you, you can buy me a coffee:

👉 https://buymeacoffee.com/darianvance

Top comments (0)