Python Program to calculate the Round Trip Time (RTT)

Here we will see how Python can be used to get the Round Trip Time (RTT). The RTT is the time taken by the entire trip of a signal ? the time between when a signal is sent and when the acknowledge signal is received.

The RTT results vary based on different parameters like ?

  • The data transfer rate of the sender's side.
  • The nature of the transmission media.
  • The actual distance between the sender and receiver.
  • The number of nodes between sender and receiver.
  • The amount of traffic on LAN.
  • Number of requests handled by intermediate points.

Example

The following code calculates RTT by measuring the time taken for an HTTP request ?

import time
import requests
import sys

def find_roundtriptime(url):
    initial_time = time.time()  # Store the time when request is sent
    request = requests.get(url)
    ending_time = time.time()  # Time when acknowledged the request
    elapsed_time = ending_time - initial_time
    print('The Round Trip Time for {} is {:.4f} seconds'.format(url, elapsed_time))

# Example usage
find_roundtriptime('https://www.tutorialspoint.com/')
find_roundtriptime('https://www.google.com')

Output

The Round Trip Time for https://www.tutorialspoint.com/ is 0.8301 seconds
The Round Trip Time for https://www.google.com is 0.5217 seconds

Enhanced Version with Error Handling

A more robust version that handles potential errors and provides better output formatting ?

import time
import requests

def calculate_rtt(url):
    try:
        initial_time = time.time()
        response = requests.get(url, timeout=10)
        ending_time = time.time()
        
        rtt = ending_time - initial_time
        print(f"URL: {url}")
        print(f"Status Code: {response.status_code}")
        print(f"Round Trip Time: {rtt:.4f} seconds")
        print("-" * 40)
        
    except requests.exceptions.RequestException as e:
        print(f"Error accessing {url}: {e}")

# Test with multiple URLs
urls = [
    'https://www.tutorialspoint.com/',
    'https://www.google.com',
    'https://www.github.com'
]

for url in urls:
    calculate_rtt(url)
URL: https://www.tutorialspoint.com/
Status Code: 200
Round Trip Time: 0.7823 seconds
----------------------------------------
URL: https://www.google.com
Status Code: 200
Round Trip Time: 0.4512 seconds
----------------------------------------
URL: https://www.github.com
Status Code: 200
Round Trip Time: 0.6134 seconds
----------------------------------------

Key Points

  • RTT Measurement: Uses time.time() before and after the HTTP request
  • Network Factors: RTT varies based on distance, network congestion, and server response time
  • Error Handling: Important for real-world applications to handle network timeouts and errors
  • Timeout Setting: Prevents hanging on unresponsive servers

Conclusion

This program demonstrates how to measure Round Trip Time using Python's time and requests modules. RTT is crucial for network performance analysis and can help identify network bottlenecks or connectivity issues.

Updated on: 2026-03-25T04:53:34+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements