aboutsummaryrefslogtreecommitdiffstats
path: root/examples/complex/websocket_inject_message.py
blob: e9c3ea0c4494ecaa61251affe0f0995da198a8c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""
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)
            flow.inject_message(flow.client_conn, 'This is the #{} an injected message!'.format(i))
            i += 1

    def websocket_start(self, flow):
        asyncio.get_event_loop().create_task(self.inject(flow))


addons = [InjectWebSocketMessage()]
ROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include "vl.h" #include "xen_console.h" #include "xenfb.h" extern void init_blktap(void); /* The Xen PV machine currently provides * - a virtual framebuffer * - .... */ static void xen_init_pv(uint64_t ram_size, int vga_ram_size, char *boot_device, DisplayState *ds, const char **fd_filename, int snapshot, const char *kernel_filename, const char *kernel_cmdline, const char *initrd_filename, const char *direct_pci) { struct xenfb *xenfb; extern int domid; #ifndef CONFIG_STUBDOM /* Initialize tapdisk client */ init_blktap(); #endif /* Connect to text console */ if (serial_hds[0]) { if (xencons_init(domid, serial_hds[0]) < 0) { fprintf(stderr, "Could not connect to domain console\n"); exit(1); } } /* Prepare PVFB state */ xenfb = xenfb_new(domid, ds); if (xenfb == NULL) { fprintf(stderr, "Could not create framebuffer (%s)\n", strerror(errno)); exit(1); } } QEMUMachine xenpv_machine = { "xenpv", "Xen Para-virtualized PC", xen_init_pv, }; /* * Local variables: * indent-tabs-mode: nil * c-indent-level: 4 * c-basic-offset: 4 * tab-width: 4 * End: */