Show progress bar in client
This commit is contained in:
parent
8bd0ac76f2
commit
4a05b05ace
@ -6,6 +6,7 @@ import os
|
|||||||
import random
|
import random
|
||||||
import ssl
|
import ssl
|
||||||
import string
|
import string
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
class Client:
|
class Client:
|
||||||
@ -102,6 +103,8 @@ class Client:
|
|||||||
writer.write(
|
writer.write(
|
||||||
f"s|{self.token}|{file_name}|{file_size}\n".encode('utf-8')
|
f"s|{self.token}|{file_name}|{file_size}\n".encode('utf-8')
|
||||||
)
|
)
|
||||||
|
self.set_file_information(file_name=file_name,
|
||||||
|
file_size=file_size)
|
||||||
await writer.drain()
|
await writer.drain()
|
||||||
# Wait for server start signal
|
# Wait for server start signal
|
||||||
while 1:
|
while 1:
|
||||||
@ -156,6 +159,7 @@ class Client:
|
|||||||
while not os.path.isfile(file_path):
|
while not os.path.isfile(file_path):
|
||||||
await asyncio.sleep(.5)
|
await asyncio.sleep(.5)
|
||||||
logging.info("Sending file...")
|
logging.info("Sending file...")
|
||||||
|
bytes_sent = 0
|
||||||
with open(file_path, 'rb') as file_to_send:
|
with open(file_path, 'rb') as file_to_send:
|
||||||
while not self.stopping:
|
while not self.stopping:
|
||||||
output_data = file_to_send.read(self.buffer_chunk_size)
|
output_data = file_to_send.read(self.buffer_chunk_size)
|
||||||
@ -172,6 +176,23 @@ class Client:
|
|||||||
logging.info('Server closed the connection.')
|
logging.info('Server closed the connection.')
|
||||||
self.stop()
|
self.stop()
|
||||||
break
|
break
|
||||||
|
bytes_sent += self.buffer_chunk_size
|
||||||
|
new_progress = min(
|
||||||
|
int(bytes_sent / self.file_size * 100),
|
||||||
|
100
|
||||||
|
)
|
||||||
|
progress_showed = (new_progress // 10) * 10
|
||||||
|
sys.stdout.write(
|
||||||
|
f"\t\t\tSending `{self.file_name}`: "
|
||||||
|
f"{'#' * (progress_showed // 10)}"
|
||||||
|
f"{'.' * ((100 - progress_showed) // 10)}\t"
|
||||||
|
f"{new_progress}% completed "
|
||||||
|
f"({min(bytes_sent, self.file_size) // 1000} "
|
||||||
|
f"of {self.file_size // 1000} KB)\r"
|
||||||
|
)
|
||||||
|
sys.stdout.flush()
|
||||||
|
sys.stdout.write('\n')
|
||||||
|
sys.stdout.flush()
|
||||||
writer.close()
|
writer.close()
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -218,11 +239,29 @@ class Client:
|
|||||||
file_path += '.enc'
|
file_path += '.enc'
|
||||||
logging.info("Receiving file...")
|
logging.info("Receiving file...")
|
||||||
with open(file_path, 'wb') as file_to_receive:
|
with open(file_path, 'wb') as file_to_receive:
|
||||||
|
bytes_received = 0
|
||||||
while not self.stopping:
|
while not self.stopping:
|
||||||
input_data = await reader.read(self.buffer_chunk_size)
|
input_data = await reader.read(self.buffer_chunk_size)
|
||||||
|
bytes_received += self.buffer_chunk_size
|
||||||
|
new_progress = min(
|
||||||
|
int(bytes_received / self.file_size * 100),
|
||||||
|
100
|
||||||
|
)
|
||||||
|
progress_showed = (new_progress // 10) * 10
|
||||||
|
sys.stdout.write(
|
||||||
|
f"\t\t\tReceiving `{self.file_name}`: "
|
||||||
|
f"{'#' * (progress_showed // 10)}"
|
||||||
|
f"{'.' * ((100 - progress_showed) // 10)}\t"
|
||||||
|
f"{new_progress}% completed "
|
||||||
|
f"({min(bytes_received, self.file_size) // 1000} "
|
||||||
|
f"of {self.file_size // 1000} KB)\r"
|
||||||
|
)
|
||||||
|
sys.stdout.flush()
|
||||||
if not input_data:
|
if not input_data:
|
||||||
break
|
break
|
||||||
file_to_receive.write(input_data)
|
file_to_receive.write(input_data)
|
||||||
|
sys.stdout.write('\n')
|
||||||
|
sys.stdout.flush()
|
||||||
logging.info("File received.")
|
logging.info("File received.")
|
||||||
if self.password:
|
if self.password:
|
||||||
logging.info("Decrypting file...")
|
logging.info("Decrypting file...")
|
||||||
@ -257,7 +296,7 @@ class Client:
|
|||||||
if file_name is not None:
|
if file_name is not None:
|
||||||
self._file_name = file_name
|
self._file_name = file_name
|
||||||
if file_size is not None:
|
if file_size is not None:
|
||||||
self._file_size = file_size
|
self._file_size = int(file_size)
|
||||||
|
|
||||||
|
|
||||||
def get_action(action):
|
def get_action(action):
|
||||||
|
@ -71,6 +71,9 @@ class Server:
|
|||||||
if connection_token not in self.buffers:
|
if connection_token not in self.buffers:
|
||||||
break
|
break
|
||||||
self.buffers[connection_token].append(input_data)
|
self.buffers[connection_token].append(input_data)
|
||||||
|
except ConnectionResetError as e:
|
||||||
|
logging.error(e)
|
||||||
|
break
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(e, exc_info=True)
|
logging.error(e, exc_info=True)
|
||||||
|
|
||||||
@ -96,6 +99,9 @@ class Server:
|
|||||||
break
|
break
|
||||||
writer.write(input_data)
|
writer.write(input_data)
|
||||||
await writer.drain()
|
await writer.drain()
|
||||||
|
except ConnectionResetError as e:
|
||||||
|
logging.error(e)
|
||||||
|
break
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(e, exc_info=True)
|
logging.error(e, exc_info=True)
|
||||||
errors += 1
|
errors += 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user