Python intercept http requests

Python Intercept HTTP Requests

Python provides several libraries for intercepting and manipulating HTTP requests. Two popular libraries for this purpose are requests and mitmproxy. Let’s discuss each of them in detail.

1. Using Requests library

The requests library is a simple and elegant HTTP library for Python, which allows you to send HTTP requests, intercept them, and modify them.

Here’s an example of intercepting an HTTP request using the requests library:


import requests

def intercept_request(request):
    print("Intercepted Request:")
    print("URL:", request.url)
    print("Headers:", request.headers)
    print("Method:", request.method)
    print("Body:", request.body)

# Send a sample request
response = requests.get("https://www.example.com")

In this example, we define a function intercept_request that takes a request object as an argument. This function is called every time a request is sent using the requests library. Inside the function, you can access various properties of the request, such as URL, headers, method, and body.

2. Using mitmproxy

mitmproxy is a powerful interactive HTTP proxy that provides capabilities for intercepting, modifying, and inspecting HTTP requests and responses.

Here’s an example of intercepting an HTTP request using mitmproxy:


from mitmproxy import proxy, options
from mitmproxy.tools.dump import DumpMaster

class Intercept:
    def request(self, flow):
        print("Intercepted Request:")
        print("URL:", flow.request.url)
        print("Headers:", flow.request.headers)
        print("Method:", flow.request.method)
        print("Body:", flow.request.content)

options = options.Options(listen_host='0.0.0.0', listen_port=8080)
m = DumpMaster(options)
m.addons.add(Intercept())

try:
    m.run()
except KeyboardInterrupt:
    m.shutdown()

In this example, we define a class Intercept that has a method request. This method is called every time an HTTP request is intercepted by mitmproxy. Inside the method, you can access various properties of the intercepted request, such as URL, headers, method, and body.

To run the script, you need to install mitmproxy using pip install mitmproxy and run it on the command line using mitmproxy -s <script.py>.
Make sure to replace <script.py> with the actual path to your script file.

These are just basic examples to get you started with intercepting HTTP requests in Python. Both the requests and mitmproxy libraries provide extensive documentation and features for more advanced use cases.

Leave a comment