Load Testing with Termux and Python

1. Installing Termux and Setting Up the Environment

First, open Termux and make sure your environment is up-to-date:

pkg update && pkg upgrade

2. Installing Required Libraries

Install Python and the required libraries using the following commands:

pkg install python pip install requests

3. Creating the Python Script

Use a text editor like nano to create a new Python script file:

nano load_test.py

In the editor, paste the following code:

import requests
import random
import time

# List of common User-Agent strings
user_agents = [
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15",
    "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0",
    "Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1",
    "Mozilla/5.0 (Linux; Android 11; SM-G991B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Mobile Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15",
]

# List of example Google search URLs
referer_urls = [
    "https://www.google.com",
    "https://www.yahoo.com",
    "https://www.bing.com",
]

def generate_random_ipv6():
    return ':'.join('%x' % random.randint(0, 2**16 - 1) for i in range(8))

def send_requests(url, number_of_requests):
    for i in range(number_of_requests):
        ipv6 = generate_random_ipv6()
        headers = {
            'User-Agent': random.choice(user_agents),  # Randomly choose a User-Agent
            'X-Forwarded-For': ipv6,
            'Referer': random.choice(referer_urls),  # Randomly choose a Google search referrer
        }
        try:
            response = requests.get(url, headers=headers)
            print(f"Request {i+1}: Status Code: {response.status_code}, IP: {ipv6}, User-Agent: {headers['User-Agent']}, Referer: {headers['Referer']}")
        except requests.exceptions.RequestException as e:
            print(f"Request {i+1}: Failed with IP: {ipv6}, User-Agent: {headers['User-Agent']}, Referer: {headers['Referer']}")
        time.sleep(0.1)  # Optional: Sleep for a short time to avoid overwhelming the server

if __name__ == "__main__":
    url = "https://www.miralishahidi.ir/"
    number_of_requests = 100000  # Adjust this number as needed
    send_requests(url, number_of_requests)
            

4. Saving and Exiting the File

To save the file and exit the editor:

5. Running the Script

Now that your script is ready, you can run it using the following command:

python load_test.py

Important Notes:

1. Installing Termux and Setting Up the Environment

First, open Termux and make sure your environment is up-to-date:

pkg update && pkg upgrade

2. Installing Required Libraries

Install Python and the required libraries using the following commands:

pkg install python pip install requests

3. Creating the Python Script

Use a text editor like nano to create a new Python script file:

nano load_test.py

In the editor, paste the following code:

import requests
import random
import time

def generate_random_ipv4():
    return '.'.join(str(random.randint(0, 255)) for _ in range(4))

def generate_random_user_agent():
    user_agents = [
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:90.0) Gecko/20100101 Firefox/90.0",
        "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64; Trident/7.0; AS; rv:11.0) like Gecko",
        "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6; rv:93.0) Gecko/20100101 Firefox/93.0",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7; rv:91.0) Gecko/20100101 Firefox/91.0",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Edge/93.0.961.52",
        "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1",
        "Mozilla/5.0 (iPad; CPU OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1",
        "Mozilla/5.0 (Android 10; Mobile; rv:91.0) Gecko/91.0 Firefox/91.0",
        "Mozilla/5.0 (Android 11; Mobile; rv:93.0) Gecko/93.0 Firefox/93.0",
        "Mozilla/5.0 (Linux; Android 10; SM-A505F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Mobile Safari/537.36",
        "Mozilla/5.0 (Linux; Android 11; SM-G991B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Mobile Safari/537.36",
        "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0",
        "Mozilla/5.0 (Windows NT 7.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36 Edge/91.0.864.48",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:87.0) Gecko/20100101 Firefox/87.0"
    ]
    return random.choice(user_agents)

def generate_random_referer():
    referers = [
        "https://www.google.com/",
        "https://www.bing.com/",
        "https://www.yahoo.com/",
        "https://www.duckduckgo.com/",
        "https://www.ask.com/",
        "https://search.yahoo.com/",
        "https://www.aol.com/",
        "https://www.baidu.com/",
        "https://www.yandex.com/"
    ]
    return random.choice(referers)

def send_requests(url, number_of_requests):
    for i in range(number_of_requests):
        ipv4 = generate_random_ipv4()
        headers = {
            'User-Agent': generate_random_user_agent(),
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Language': 'en-US,en;q=0.5',
            'Connection': 'keep-alive',
            'Upgrade-Insecure-Requests': '1',
            'Referer': generate_random_referer(),  # Set a random Referer
            'X-Forwarded-For': ipv4  # Add the random IPv4 address
        }
        
        start_time = time.time()
        try:
            response = requests.get(url, headers=headers)
            elapsed_time = time.time() - start_time
            print(f"Request {i+1}: IPv4: {ipv4}, User-Agent: {headers['User-Agent']}, Status Code: {response.status_code}, Time Taken: {elapsed_time:.2f} seconds, Response Size: {len(response.content)} bytes")
        except requests.RequestException as e:
            print(f"Request {i+1} failed: {e}")

if __name__ == '__main__':
    url = 'https://www.miralishahidi.ir'
    number_of_requests = 1000000
    send_requests(url, number_of_requests)
            

4. Saving and Exiting the File

To save the file and exit the editor:

5. Running the Script

Now that your script is ready, you can run it using the following command:

python load_test.py

Important Notes: