-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
49 lines (47 loc) · 1.79 KB
/
client.py
File metadata and controls
49 lines (47 loc) · 1.79 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
import asyncio
import datetime
import hashlib
import os
import time
import traceback
import websockets
import pickle
import os
from websockets.exceptions import ConnectionClosedError,InvalidURI
async def hello():
clear_command = {
"nt": "cls",
"posix": "clear"
}
host = input("Enter host ip: ")
while True:
try:
uri = f"ws://{host}:8765"
async with websockets.connect(uri) as websocket:
while True:
message = hashlib.sha256(bytes(str(time.mktime(datetime.datetime.now().utctimetuple())).encode("utf-8"))).hexdigest()
await websocket.send(message)
os.system(clear_command[os.name])
feedback = await websocket.recv()
data = pickle.loads(feedback)
print(f"<<< {data['payload']}")
await asyncio.sleep(data['wait_time'])
except ConnectionRefusedError:
print("Could not find an active server! Retrying connection in 5 seconds...")
await asyncio.sleep(5)
except ConnectionClosedError:
print("Server closed! Retrying connection in 5 seconds...")
except KeyboardInterrupt:
print("Exiting program.")
exit()
except InvalidURI:
print("Invalid URL!")
host = input("Enter new host ip: ")
except:
print("Unknown error occurred! Retrying connection in 5 seconds...")
with open("logs.txt", "w") as handle:
traceback.print_exc(file=handle)
handle.write(datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"))
await asyncio.sleep(10)
if __name__ == "__main__":
asyncio.run(hello())