-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.py
More file actions
75 lines (66 loc) · 2.34 KB
/
Copy pathproxy.py
File metadata and controls
75 lines (66 loc) · 2.34 KB
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
65
66
67
68
69
70
71
72
73
74
75
'''
proxy.py
'''
import asyncio
import websockets
import json
async def handle_browser_client(websocket):
"""Handle WebSocket connections from browsers"""
try:
# Connect to MiniKafka broker
reader, writer = await asyncio.open_connection('127.0.0.1', 9092)
async def forward_to_kafka(message):
"""Send messages to Kafka"""
try:
writer.write(json.dumps(message).encode() + b'\n')
await writer.drain()
except ConnectionError:
print("Kafka connection lost")
raise
async def forward_to_browser():
"""Forward messages to browser"""
try:
while True:
data = await reader.readuntil(b'\n')
try:
await websocket.send(data.decode())
except websockets.ConnectionClosed:
print("Browser disconnected")
break
except (ConnectionResetError, asyncio.IncompleteReadError):
print("Kafka connection closed")
# Process messages in both directions
try:
async for message in websocket:
await forward_to_kafka(json.loads(message))
# Start forwarding after initial setup
await forward_to_browser()
except websockets.ConnectionClosed:
print("Browser client disconnected normally")
except json.JSONDecodeError:
print("Received invalid JSON")
finally:
writer.close()
await writer.wait_closed()
except ConnectionRefusedError:
print("Failed to connect to Kafka broker at 127.0.0.1:9092")
await websocket.close(1011, "Backend unavailable")
async def main():
async with websockets.serve(
handle_browser_client,
"localhost",
8765,
ping_interval=30,
ping_timeout=30,
close_timeout=10
):
print("WebSocket proxy running on ws://localhost:8765")
await asyncio.Future() # Run forever
if __name__ == "__main__":
asyncio.run(main())
'''
is this correct
mini.py should be running on 127.0.0.1:9092
demo.html should be running on localhost.8000 via http.server
proxy.py should be running on localhost:8765
'''