-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
29 lines (24 loc) · 836 Bytes
/
Copy pathagent.py
File metadata and controls
29 lines (24 loc) · 836 Bytes
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
import asyncio
import websockets
import requests
import os
host = os.environ.get("CONTAINER_HOST")
async def listen_for_commands():
uri = scheme + "://" + host + port
try:
async with websockets.connect(uri) as websocket:
while True:
command = await websocket.recv()
print(f"{command}")
except websockets.exceptions.ConnectionClosedError:
print("Agent disconnected. Reconnecting...")
if __name__ == "__main__":
response = requests.get(host + "/agent/handshake").json()
if response["message"] == "OK" and response["code"] == 200:
# Awesome! Ready to connect.
port = int(response["port"])
scheme = response["scheme"]
else:
print("Error: Agent handshake failed.")
exit(1)
asyncio.run(listen_for_commands())