mitmproxy/examples/complex/websocket_inject_message.py

24 lines
692 B
Python
Raw Normal View History

2018-05-12 12:04:47 +00:00
"""
This example shows how to inject a WebSocket message to the client.
Every new WebSocket connection will trigger a new asyncio task that
periodically injects a new message to the client.
"""
import asyncio
import mitmproxy.websocket
class InjectWebSocketMessage:
async def inject(self, flow: mitmproxy.websocket.WebSocketFlow):
i = 0
while not flow.ended and not flow.error:
await asyncio.sleep(5)
2020-04-04 13:31:38 +00:00
flow.inject_message(flow.client_conn, 'This is the #{} injected message!'.format(i))
2018-05-12 12:04:47 +00:00
i += 1
def websocket_start(self, flow):
asyncio.get_event_loop().create_task(self.inject(flow))
addons = [InjectWebSocketMessage()]