-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertAudioFilesServer.py
125 lines (100 loc) · 4.21 KB
/
convertAudioFilesServer.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import sys, os, time, socket, select
import pydub
import io
class SocketData:
"""
Data associated with any given socket.
===Attributes===
content: the string denoting the content that has been received or is
to be sent
content_bytes: the bytes object denoting the content that has been received
or is to be sent
read_complete: a flag to check whether the message is complete or not
bytes_sent: denotes the number of bytes sent back to the client
"""
content: str
content_bytes: bytes
read_complete: bool
write_complete: bool
output_file = io.BytesIO
to_send: bytes
bytes_sent: int
def __init__(self):
"""
Initialize a Socket Data object
"""
self.content = ''
self.content_bytes = b''
self.read_complete = False
self.write_complete = False
self.to_send = b''
self.bytes_sent = 0
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("localhost", 8000))
# Create a TCP/IP socket
# Listen for incoming connections
sock.listen(5)
# source: https://pymotw.com/3/socket/tcp.html
inputs = [sock]
outputs = []
msg_queue = {}
# Source: https://steelkiwi.com/blog/working-tcp-sockets/
while inputs:
readable, writable, exceptional = select.select(inputs, outputs, inputs, 5)
# source: https://pymotw.com/3/socket/tcp.html
for read_from in readable:
# Source: https://steelkiwi.com/blog/working-tcp-sockets/
if read_from is sock:
# Socket is the server, we accept a browser socket
# Accept a socket
connection, client_address = sock.accept()
# source: https://pymotw.com/3/socket/tcp.html
# Set the socket to nonblocking, add socket and create data object
connection.setblocking(False)
inputs.append(connection)
msg_queue[connection] = SocketData()
# source: https://steelkiwi.com/blog/working-tcp-sockets/
else:
if not msg_queue[read_from].read_complete:
try:
# print("waiting...")
data = read_from.recv(16)
msg_queue[read_from].content_bytes += data
if len(msg_queue[read_from].content_bytes) >= 32 and b"EOF\r\n\r\n" in msg_queue[read_from].content_bytes[-32:]:
print("File read!")
msg_queue[read_from].read_complete = True
outputs.append(read_from)
inputs.remove(read_from)
request_list = msg_queue[read_from].content_bytes.split(b"\r\n")
file_format = request_list[3][-3:].decode('utf-8')
content_index = request_list.index(b"CONTENT")
old_file_content = b"\r\n".join(request_list[content_index+1:-3])
received_file = io.BytesIO(old_file_content)
audio = pydub.AudioSegment.from_file(received_file,
format=file_format)
msg_queue[read_from].output_file = io.BytesIO()
msg_queue[read_from].output_file = audio.export(msg_queue[read_from].output_file, 'wav')
msg_queue[read_from].output_file.seek(0)
finally:
pass
for write_to in writable:
if not msg_queue[write_to].write_complete:
try:
# print("sending...")
msg_queue[write_to].to_send = msg_queue[write_to].output_file.read(16)
# print(msg_queue[write_to].to_send)
write_to.send(msg_queue[write_to].to_send)
msg_queue[write_to].bytes_sent += 16
if not msg_queue[write_to].to_send:
msg_queue[write_to].write_complete = True
write_to.send(b"\r\nEOF\r\n\r\n")
print("Finished Sending!")
finally:
pass
for s in exceptional:
inputs.remove(s)
if s in outputs:
outputs.remove(s)
s.close()
del msg_queue[s]
# Source: https://steelkiwi.com/blog/working-tcp-sockets/