diff options
Diffstat (limited to 'day4/task4/server.py')
| -rw-r--r-- | day4/task4/server.py | 10 |
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() |