-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.py
48 lines (39 loc) · 1.5 KB
/
buffer.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
class Buffer:
def __init__(self, s, buffer_size=4096):
'''Buffer a pre-created socket.
'''
self.sock = s
self.buffer_size = buffer_size
def get_bytes(self, f, progress=None):
"""Write the new file at progress with buffer size"""
data = self.sock.recv(self.buffer_size)
while data:
f.write(data)
data = self.sock.recv(self.buffer_size)
# update the progress bar if progress not None
if progress:
progress.update(len(data))
# close the client socket
self.sock.close()
print('File received successfully.')
def put_bytes(self, f, progress=None):
while True:
# read the bytes from the file
bytes_read = f.read(self.buffer_size)
if not bytes_read:
# file transmitting is done
break
# we use sendall to assure transimission in
# busy networks
self.sock.sendall(bytes_read)
# update the progress bar if progress not None
if progress:
progress.update(len(bytes_read))
print('File sended successfully.')
def get_utf8(self):
"""Get file metadata from client and decode to utf-8 string."""
received = self.sock.recv(self.buffer_size).decode("ISO-8859-1")
return received
def put_utf8(self, meta):
"""Send file metadata as bytes."""
self.sock.send(meta.encode())