summaryrefslogtreecommitdiff
path: root/day4/task4/server.py
diff options
context:
space:
mode:
authorAndrew <saintruler@gmail.com>2019-04-22 21:00:47 +0400
committerAndrew <saintruler@gmail.com>2019-04-22 21:00:47 +0400
commit7e591a381db30bfcd374ebdffdc058f8bbfd24ed (patch)
tree70ecc2b57b26eda031a6b611d04f1d69885af32d /day4/task4/server.py
parentfae0c2ca1055e2c94299d16b12a20e84fbae1845 (diff)
Четвертая задача завершена. Используется не обязательная библиотека tqdm (чтобы было красиво)
Diffstat (limited to 'day4/task4/server.py')
-rw-r--r--day4/task4/server.py10
1 files changed, 8 insertions, 2 deletions
diff --git a/day4/task4/server.py b/day4/task4/server.py
index 980eebc..fb88629 100644
--- a/day4/task4/server.py
+++ b/day4/task4/server.py
@@ -35,18 +35,21 @@ def handle_request(data, connection):
# Я знаю, что это плохо, но сервер не должен падать,
# а других ошибок я не смог вспомнить
except Exception as e:
- header = json.dumps({'status': 2, 'reason': str(e)})
+ header = json.dumps({'status': 2, 'reason': f'{type(e)}: {e}'})
connection.sendall(pack_str(header))
else:
length = len(file_data)
header = json.dumps({'status': 0, 'size': length, 'path': request['path']})
connection.sendall(pack_str(header))
+ print(f'Started sending file of size {length} bytes...')
i = 0
while i < length:
packet = pack_bytes(file_data[i: i + CHUNK_SIZE])
connection.sendall(packet)
i += CHUNK_SIZE
+ print(i / length * 100, '%')
+ print(f'Transmission ended')
def get_data(connection):
@@ -91,7 +94,7 @@ def handle_connection(connection, address):
HOST, PORT = ADDR = '0.0.0.0', 6000
-CHUNK_SIZE = 4096
+CHUNK_SIZE = 4096000
sock = socket.socket()
sock.bind(ADDR)
sock.listen(5)
@@ -103,6 +106,9 @@ while True:
print(f'Got a connection from {address[0]}')
handle_connection(connection, address)
+ except ConnectionResetError:
+ print(f'Connection with {address[0]} was unexpectedly closed')
+
except KeyboardInterrupt:
print('Stopping server...')
sock.close()