-
-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathwebsocket_client.py
64 lines (54 loc) · 1.66 KB
/
websocket_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
"""
proxy.py
~~~~~~~~
⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on
Network monitoring, controls & Application development, testing, debugging.
:copyright: (c) 2013-present by Abhinav Singh and contributors.
:license: BSD, see LICENSE for more details.
"""
import time
import logging
from proxy.http.websocket import (
WebsocketFrame, WebsocketClient, websocketOpcodes,
)
from proxy.common.constants import DEFAULT_LOG_FORMAT
logging.basicConfig(level=logging.INFO, format=DEFAULT_LOG_FORMAT)
# globals
client: WebsocketClient
last_dispatch_time: float
static_frame = memoryview(WebsocketFrame.text(b'hello'))
num_echos = 10
logger = logging.getLogger(__name__)
def on_message(frame: WebsocketFrame) -> None:
"""WebsocketClient on_message callback."""
global client, num_echos, last_dispatch_time
logger.info(
'Received %r after %d millisec' %
(frame.data, (time.time() - last_dispatch_time) * 1000),
)
assert(
frame.data == b'hello' and frame.opcode ==
websocketOpcodes.TEXT_FRAME
)
if num_echos > 0:
client.queue(static_frame)
last_dispatch_time = time.time()
num_echos -= 1
else:
client.close()
if __name__ == '__main__':
# Constructor establishes socket connection
client = WebsocketClient(
b'localhost',
8899,
b'/ws-route-example',
on_message=on_message,
)
# Perform handshake
client.handshake()
# Queue some data for client
client.queue(static_frame)
last_dispatch_time = time.time()
# Start event loop
client.run()