-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
31 lines (26 loc) · 967 Bytes
/
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
import socket
import time
import wave
import const
def stream_wav_to_server():
with socket.socket() as client_socket:
client_socket.connect((const.HOST, const.PORT))
with wave.open(const.SAMPLE_WAV_FILE, 'r') as wave_file:
print('Started streaming')
while True:
try:
print('.', end='', flush=True)
time.sleep(1)
data = wave_file.readframes(const.FRAME_RATE)
if not data:
print('\nFile streamed completely. Exiting')
break
client_socket.send(data)
except KeyboardInterrupt:
print('\nExiting')
break
except (BrokenPipeError, ConnectionResetError):
print('\nConnection lost. Exiting')
break
if __name__ == '__main__':
stream_wav_to_server()