From 7e591a381db30bfcd374ebdffdc058f8bbfd24ed Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 22 Apr 2019 21:00:47 +0400 Subject: =?UTF-8?q?=D0=A7=D0=B5=D1=82=D0=B2=D0=B5=D1=80=D1=82=D0=B0=D1=8F?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B0=20=D0=B7=D0=B0=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D1=88=D0=B5=D0=BD=D0=B0.=20=D0=98=D1=81=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=B7=D1=83=D0=B5=D1=82=D1=81=D1=8F=20=D0=BD?= =?UTF-8?q?=D0=B5=20=D0=BE=D0=B1=D1=8F=D0=B7=D0=B0=D1=82=D0=B5=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D0=B0=D1=8F=20=D0=B1=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE=D1=82?= =?UTF-8?q?=D0=B5=D0=BA=D0=B0=20tqdm=20(=D1=87=D1=82=D0=BE=D0=B1=D1=8B=20?= =?UTF-8?q?=D0=B1=D1=8B=D0=BB=D0=BE=20=D0=BA=D1=80=D0=B0=D1=81=D0=B8=D0=B2?= =?UTF-8?q?=D0=BE)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day4/task4/server.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'day4/task4/server.py') 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() -- cgit v1.2.3