|
| 1 | +# Multithreaded HTTP Requests with Python |
| 2 | + |
| 3 | +This script performs multithreaded HTTP requests using Python's `requests` library and `ThreadPoolExecutor`. The code allows you to send multiple HTTP requests concurrently for a specified duration. |
| 4 | + |
| 5 | +## Features |
| 6 | +- Sends concurrent HTTP requests to a specified URL. |
| 7 | +- Supports multiple request methods (GET, POST, etc.). |
| 8 | +- Allows customizing headers, cookies, data, and URL parameters. |
| 9 | +- The number of threads and the duration of the requests can be controlled. |
| 10 | +- Uses ThreadPoolExecutor to handle multithreading efficiently. |
| 11 | + |
| 12 | +## Requirements |
| 13 | +- Python 3.x |
| 14 | +- `requests` library |
| 15 | + |
| 16 | +To install the `requests` library, run: |
| 17 | +```bash |
| 18 | +pip install requests |
| 19 | +``` |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +### Function: `make_request` |
| 24 | +This function sends a single HTTP request to a given URL with the specified parameters. |
| 25 | +- **Arguments**: |
| 26 | + - `url` (str): The URL to send the request to. |
| 27 | + - `method` (str): The HTTP method to use (default: GET). |
| 28 | + - `headers` (dict): Optional HTTP headers to include. |
| 29 | + - `cookies` (dict): Optional cookies to include. |
| 30 | + - `data` (dict): Optional data for POST requests. |
| 31 | + - `params` (dict): Optional URL parameters. |
| 32 | + |
| 33 | +- **Example**: |
| 34 | +make_request("https://example.com", method='POST', data={"key": "value"}) |
| 35 | + |
| 36 | +### Function: `start_requests` |
| 37 | +This function sends multiple HTTP requests concurrently for a specified duration. |
| 38 | +- **Arguments**: |
| 39 | + - `url` (str): The URL to send the requests to. |
| 40 | + - `method` (str): The HTTP method to use (default: GET). |
| 41 | + - `headers` (dict): Optional HTTP headers to include. |
| 42 | + - `cookies` (dict): Optional cookies to include. |
| 43 | + - `data` (dict): Optional data for POST requests. |
| 44 | + - `params` (dict): Optional URL parameters. |
| 45 | + - `num_threads` (int): The number of threads to use (default: 5). |
| 46 | + - `duration` (int): The duration in seconds to send requests (default: 10 seconds). |
| 47 | + |
| 48 | +- **Example**: |
| 49 | +url = "https://example.com/api" |
| 50 | +start_requests(url, method='GET', num_threads=5, duration=15) |
| 51 | + |
| 52 | +## How It Works |
| 53 | +1. The `start_requests` function uses `ThreadPoolExecutor` to manage the specified number of threads. |
| 54 | +2. Each thread sends an HTTP request to the given URL. |
| 55 | +3. The process continues until the specified duration ends. |
| 56 | +4. The `make_request` function handles sending the requests and printing the response status codes. |
| 57 | + |
| 58 | +## Example Usage |
| 59 | +url = "https://example.com/api" |
| 60 | +start_requests(url, num_threads=5, duration=15) # Sends requests for 15 seconds using 5 threads. |
| 61 | + |
| 62 | +## Customization |
| 63 | +- You can modify the method, headers, cookies, data, or params in the function calls to fit your use case. |
| 64 | +- For POST requests, pass data through the `data` argument. |
| 65 | + |
| 66 | +start_requests("https://example.com/post", method='POST', data={'key': 'value'}, num_threads=10, duration=20) |
| 67 | + |
| 68 | +## License |
| 69 | +This project is licensed under the MIT License. |
0 commit comments