A Python module for intercepting HTTP/HTTPS requests using mitmproxy
Project description
MITM Proxy Module
A Python module that provides a simple interface for intercepting HTTP/HTTPS requests using mitmproxy, with the ability to capture request/response data and generate curl commands.
Features
- Easy-to-use Python interface for mitmproxy
- Context manager support for automatic cleanup
- Thread-safe request capture storage
- Parallel request support
- Full curl command generation
- URL-based capture retrieval
- Configurable proxy settings
- Comprehensive error handling and logging
Installation
This project uses an existing venv with mitmproxy pre-installed:
# Activate the existing venv
source venv/bin/activate # Linux/Mac
# or
venv\Scripts\activate # Windows
# Install additional required packages
pip install requests
Quick Start
from mitm_requests import MITMProxy
import requests
# Basic usage with context manager
with MITMProxy() as proxy:
proxies = {'http': proxy.get_proxy_url(), 'https': proxy.get_proxy_url()}
requests.get('https://httpbin.org/get', proxies=proxies, verify=False)
capture = proxy.get('https://httpbin.org/get')
if capture:
print(f"Response: {capture.body}")
print(f"Curl: {capture.curl}")
API Reference
MITMProxy Class
__init__(port=8080, host='127.0.0.1'): Initialize proxystart(): Start the proxy serverstop(): Stop the proxy serverget_proxy_url(): Get proxy URL for requestsget(url): Get capture data for specific URLget_all_captures(): Get all captured dataclear_captures(): Clear all capturesis_running(): Check if proxy is running
CaptureData Class
url: Request URLmethod: HTTP methodstatus_code: Response status coderequest_body: Request body contentresponse_body/body: Response body contentcurl: Generated curl commandrequest_headers/response_headers: HTTP headers
Running Tests
# Run unit tests
python -m pytest tests/ -v
# Run specific test file
python -m pytest tests/test_proxy.py -v
# Run with coverage
python -m pytest tests/ --cov=src/mitm_proxy
Examples
Capturing a Single Request
This example demonstrates how to capture data for a specific URL using the MITMProxy with SeleniumBase.
import requests
import time
from src.mitm_requests import MITMProxy
print("\n=== Example: Capturing a Single Request ===")
with MITMProxy() as proxy:
print(f"Proxy running on: {proxy.get_proxy_url()}")
finra_url = "https://www.finra.org/finra-data/fixed-income/bond?cusip=912810TZ1&bondType=TS"
finra_bond_url = "https://services-dynarep.ddwa.finra.org/public/reporting/v2/data/group/FixedIncomeMarket/name/TreasuryEndOfDayPriceYield"
from seleniumbase import SB
with SB(proxy=proxy.get_proxy_url(), chromium_arg="--ignore-certificate-errors", headless=False, uc=True) as sb:
sb.open(finra_url)
sb.sleep(3)
# Get captured data for specific URL
time.sleep(0.5) # Allow time for capture
capture = proxy.get(finra_bond_url)
if capture:
print(f"\n=== Captured Data ===")
print(f"URL: {capture.url}")
print(f"Method: {capture.method}")
print(f"Status: {capture.status_code}")
print(f"Response Body (first 200 chars): {capture.body[:200]}...")
print(f"\nCurl Command:\n{capture.curl}")
print(f"\nRaw Response:\n{capture.raw_response.decode('utf-8', errors='replace')}")
else:
print("No capture found for the URL")
Capturing All Requests
This example shows how to capture and display data for all intercepted requests.
import requests
import time
from src.mitm_requests import MITMProxy
print("\n=== Example: Capturing All Requests ===")
with MITMProxy() as proxy:
print(f"Proxy running on: {proxy.get_proxy_url()}")
finra_url = "https://www.finra.org/finra-data/fixed-income/bond?cusip=912810TZ1&bondType=TS"
finra_bond_url = "https://services-dynarep.ddwa.finra.org/public/reporting/v2/data/group/FixedIncomeMarket/name/TreasuryEndOfDayPriceYield"
from seleniumbase import SB
with SB(proxy=proxy.get_proxy_url(), chromium_arg="--ignore-certificate-errors", headless=False, uc=True) as sb:
sb.open(finra_url)
sb.sleep(3)
# Get all captured data
time.sleep(0.5) # Allow time for captures
captures = proxy.get_all_captures()
print(f"Captured {len(captures)} requests")
for capture in captures:
print(f"\n=== Captured Data ===")
print(f"URL: {capture.url}")
print(f"Method: {capture.method}")
print(f"Status: {capture.status_code}")
print(f"Response Body (first 200 chars): {capture.body[:200]}...")
print(f"\nCurl Command:\n{capture.curl}")
print(f"\nRaw Response:\n{capture.raw_response.decode('utf-8', errors='replace')}")
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mitm_requests-1.0.0.tar.gz.
File metadata
- Download URL: mitm_requests-1.0.0.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2cfabb0cbe483c4ef6711b02292e54b5db1f755a01dacb4d61eb9a78f6bf190c
|
|
| MD5 |
46931d1d28fce0c574dfe2c815420072
|
|
| BLAKE2b-256 |
ae2264c0a19ea36c4c713eee81825ac829a28a92e968976a82505a40b0676742
|
File details
Details for the file mitm_requests-1.0.0-py3-none-any.whl.
File metadata
- Download URL: mitm_requests-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3f74522ee00bc913a5da1053a2ed6a25e49c3cdaaf1d8825cc0f94a2412f9c74
|
|
| MD5 |
d73e51b08e74c83214f6e0944809f793
|
|
| BLAKE2b-256 |
82f5fb345697be0e5d25e38fc1f73ce0ddc7776c2fef2f4b5d23e10fadfe2061
|