-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
68 lines (53 loc) · 1.58 KB
/
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
65
66
67
68
import asyncio
from hypercorn.config import Config
from hypercorn.asyncio import serve
from quart import Quart, request
from logging.config import dictConfig
from pgm_multicast.client import ClientProtocol, ClientTransport
from pgm_multicast.config import transport_conf
def setup_logging():
return dictConfig(
{
"version": 1,
"formatters": {
"default": {
"format": "[%(asctime)s] %(levelname)s in %(module)s: %(message)s",
}
},
"handlers": {
"wsgi": {
"class": "logging.StreamHandler",
"formatter": "default",
}
},
"root": {"level": "DEBUG", "handlers": ["wsgi"]},
}
)
setup_logging()
app = Quart(__name__)
app.config.from_mapping(
SECRET_KEY="dev",
)
config = Config()
config.bind = ["0.0.0.0:9001"]
config.debug = False
client_protocol = None
@app.before_serving
async def setup():
global client_protocol
protocol = ClientProtocol()
event_loop = asyncio.get_running_loop()
transport = ClientTransport(event_loop, protocol, transport_conf)
protocol.init_connection(transport)
await asyncio.sleep(2)
client_protocol = protocol
@app.route("/health")
async def health():
return {"message": "OK"}
@app.route("/send_mc", methods=["POST"])
async def send():
data = await request.get_json()
await client_protocol.send_message(data, ["192.168.1.3"])
return {"message": "OK"}
if __name__ == "__main__":
asyncio.run(serve(app, config))