Overview
Event hooks are methods that addons implement to respond to specific events in the proxy lifecycle. Mitmproxy automatically calls these methods when the corresponding event occurs.All hooks are defined as dataclass-based
Hook subclasses in the mitmproxy codebase. The hook name is automatically derived from the class name.Lifecycle Hooks
These hooks manage the addon and proxy lifecycle.load
Called when an addon is first loaded. Use this to register options and commands.loader:Loader- Provides methods to add options and commands
mitmproxy/addonmanager.py:120
configure
Called when configuration changes. Fires during startup with all options in the updated set.updated:set[str]- Set of option names that changed
mitmproxy/hooks.py:58
running
Called when the proxy is completely up and running. All addons are loaded and options are set.mitmproxy/hooks.py:81
done
Called when the addon shuts down. This is the final event an addon sees.mitmproxy/hooks.py:69
update
Called when flow objects have been modified, usually from a different addon.flows:Sequence[Flow]- List of modified flow objects
mitmproxy/hooks.py:89
HTTP Hooks
These hooks handle HTTP/HTTPS traffic.requestheaders
HTTP request headers were successfully read. The body is empty at this point.flow:http.HTTPFlow- The HTTP flow (body is empty)
- Early header inspection
- Setting streaming mode
- Blocking requests early
mitmproxy/proxy/layers/http/_hooks.py:8
request
The full HTTP request has been read.flow:http.HTTPFlow- Complete HTTP request
If request streaming is active, this fires after the entire body has been streamed. HTTP trailers can still be modified.
mitmproxy/proxy/layers/http/_hooks.py:18
responseheaders
HTTP response headers were successfully read. The body is empty at this point.flow:http.HTTPFlow- The HTTP flow (response body is empty)
mitmproxy/proxy/layers/http/_hooks.py:33
response
The full HTTP response has been read.flow:http.HTTPFlow- Complete HTTP response
mitmproxy/proxy/layers/http/_hooks.py:42
error
An HTTP error has occurred (e.g., invalid server responses, interrupted connections).flow:http.HTTPFlow- Flow with error set
Every flow receives either
error or response, but not both.mitmproxy/proxy/layers/http/_hooks.py:56
HTTP CONNECT Hooks
These hooks handle HTTP CONNECT requests for proxy tunneling.http_connect
An HTTP CONNECT request was received.flow:http.HTTPFlow- The CONNECT request
Setting a non-2xx response returns it to the client and aborts the connection.
mitmproxy/proxy/layers/http/_hooks.py:70
http_connect_upstream
An HTTP CONNECT request is about to be sent to an upstream proxy.mitmproxy/proxy/layers/http/_hooks.py:87
Connection Hooks
These hooks handle client and server connections.client_connected
A client has connected to mitmproxy.client:connection.Client- The client connection
mitmproxy/proxy/server_hooks.py:8
client_disconnected
A client connection has been closed.client:connection.Client- The closed client connection
mitmproxy/proxy/server_hooks.py:20
server_connect
Mitmproxy is about to connect to a server.data.server:connection.Server- The server connectiondata.client:connection.Client- The client on the other end
mitmproxy/proxy/server_hooks.py:39
server_connected
Mitmproxy has connected to a server.mitmproxy/proxy/server_hooks.py:51
server_disconnected
A server connection has been closed.mitmproxy/proxy/server_hooks.py:60
server_connect_error
Mitmproxy failed to connect to a server.Every server connection receives either
server_connected or server_connect_error, but not both.mitmproxy/proxy/server_hooks.py:69
WebSocket Hooks
These hooks handle WebSocket connections.websocket_start
A WebSocket connection has commenced.flow:http.HTTPFlow- The HTTP flow that initiated WebSocket
mitmproxy/proxy/layers/websocket.py:24
websocket_message
A WebSocket message was received.flow:http.HTTPFlow- Flow containing WebSocket messages
Opcode.TEXT- Text messageOpcode.BINARY- Binary message
mitmproxy/proxy/layers/websocket.py:33
websocket_end
A WebSocket connection has ended.flow:http.HTTPFlow- The completed WebSocket flow
mitmproxy/proxy/layers/websocket.py:45
TCP Hooks
These hooks handle raw TCP connections.tcp_start
A TCP connection has started.flow:tcp.TCPFlow- The TCP flow
mitmproxy/proxy/layers/tcp.py:17
tcp_message
A TCP connection has received a message.flow:tcp.TCPFlow- Flow with the latest message inflow.messages[-1]
mitmproxy/proxy/layers/tcp.py:26
tcp_end
A TCP connection has ended.mitmproxy/proxy/layers/tcp.py:36
tcp_error
A TCP error has occurred.Every TCP flow receives either
tcp_error or tcp_end, but not both.mitmproxy/proxy/layers/tcp.py:45
DNS Hooks
These hooks handle DNS queries and responses.dns_request
A DNS query has been received.flow:dns.DNSFlow- The DNS flow
mitmproxy/proxy/layers/dns.py:20
dns_response
A DNS response has been received or set.flow:dns.DNSFlow- Flow with DNS response
mitmproxy/proxy/layers/dns.py:29
dns_error
A DNS error has occurred.flow:dns.DNSFlow- Flow with error
mitmproxy/proxy/layers/dns.py:38
Hook Naming Convention
Hook names are automatically derived from their class names:- Remove “Hook” suffix
- Insert underscores before capital letters
- Convert to lowercase
mitmproxy/hooks.py:36
Deprecated Hooks
clientconnect→ Useclient_connectedclientdisconnect→ Useclient_disconnectedserverconnect→ Useserver_connectandserver_connectedserverdisconnect→ Useserver_disconnectedadd_log→ Use Python’s built-inloggingmodule
Event Ordering
Typical event sequence for an HTTP flow:With streaming enabled, event order may change. For example,
response may occur before request if the server replies with “413 Payload Too Large” during upload.Next Steps
Examples
See real-world examples using these hooks
Built-in Addons
Learn from built-in addon implementations
