Skip to main content

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 proxy
  • start(): Start the proxy server
  • stop(): Stop the proxy server
  • get_proxy_url(): Get proxy URL for requests
  • get(url): Get capture data for specific URL
  • get_all_captures(): Get all captured data
  • clear_captures(): Clear all captures
  • is_running(): Check if proxy is running

CaptureData Class

  • url: Request URL
  • method: HTTP method
  • status_code: Response status code
  • request_body: Request body content
  • response_body / body: Response body content
  • curl: Generated curl command
  • request_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

Metadata

Release files for mitm-requests 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mitm-requests 1.0.0
File Size Uploaded Provenance Actions
mitm_requests-1.0.0.tar.gz 8.7 kB Without provenance Details for mitm_requests-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mitm-requests 1.0.0
File Interpreter ABI Platform Actions
mitm_requests-1.0.0-py3-none-any.whl Python 3 none any Details for mitm_requests-1.0.0-py3-none-any.whl

Total release size: 15.5 kB

Release files / mitm_requests-1.0.0.tar.gz

Download URL mitm_requests-1.0.0.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2cfabb0cbe483c4ef6711b02292e54b5db1f755a01dacb4d61eb9a78f6bf190c
BLAKE2b-256 checksum
How to use checksums
ae2264c0a19ea36c4c713eee81825ac829a28a92e968976a82505a40b0676742
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Release files / mitm_requests-1.0.0-py3-none-any.whl

Download URL mitm_requests-1.0.0-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3f74522ee00bc913a5da1053a2ed6a25e49c3cdaaf1d8825cc0f94a2412f9c74
BLAKE2b-256 checksum
How to use checksums
82f5fb345697be0e5d25e38fc1f73ce0ddc7776c2fef2f4b5d23e10fadfe2061
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Security for mitm-requests 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page