-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwss-server.py
More file actions
36 lines (29 loc) · 1.04 KB
/
wss-server.py
File metadata and controls
36 lines (29 loc) · 1.04 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
import asyncio
import pathlib
import ssl
import websockets
import settings
import sys
async def listening(websocket, path):
gesture_type = sys.argv[1]
sample_number = int(sys.argv[2])
# prev_sample_number = sample_number
while True:
file_path = f'./data/data_{gesture_type}_{sample_number}.txt'
client_msg = await websocket.recv()
print(f'Message from client: {client_msg}')
await websocket.send(f'Received!')
if client_msg != 'end':
with open(file_path, 'a') as output_file:
output_file.write(client_msg + '\n')
else:
sample_number += 1
print(f'Server on: {settings.IP_ADDRESS}:{settings.PORT}')
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
localhost_pem = pathlib.Path(__file__).with_name('localhost.pem')
ssl_context.load_cert_chain(localhost_pem)
start_server = websockets.serve(
listening, settings.IP_ADDRESS, settings.PORT, ssl=ssl_context
)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()