-
Notifications
You must be signed in to change notification settings - Fork 0
/
async_server.py
45 lines (35 loc) · 1.23 KB
/
async_server.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
import asyncio
import wave
from datetime import datetime
import const
shutdown = False
async def handle_audio_stream(reader, writer):
address = writer.get_extra_info('peername')
print(f'Receiving stream from {address}')
time = datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
with wave.open(f'{const.OUTPUT_DIR}/{address[1]}__{time}.wav', 'w') as wav:
wav.setnchannels(const.CHANNELS)
wav.setsampwidth(const.SAMPLE_WIDTH)
wav.setframerate(const.FRAME_RATE)
while not shutdown:
data = await reader.read(4096)
if not data:
break
wav.writeframesraw(data)
print(f'Stream closed: {address}')
writer.close()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
coro = asyncio.start_server(handle_audio_stream, const.HOST, const.PORT, loop=loop)
server = loop.run_until_complete(coro)
print('Serving on {}'.format(server.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
print('\nShutting down')
shutdown = True
server.close()
pending = asyncio.all_tasks(loop=loop)
loop.run_until_complete(asyncio.gather(*pending, server.wait_closed()))
loop.close()