Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import sys
- import io
- import os
- def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█'):
- """
- Call in a loop to create terminal progress bar
- @params:
- iteration - Required : current iteration (Int)
- total - Required : total iterations (Int)
- prefix - Optional : prefix string (Str)
- suffix - Optional : suffix string (Str)
- decimals - Optional : positive number of decimals in percent complete (Int)
- length - Optional : character length of bar (Int)
- fill - Optional : bar fill character (Str)
- """
- percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
- filledLength = int(length * iteration // total)
- bar = fill * filledLength + '-' * (length - filledLength)
- print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end='\r')
- sys.stdout.flush()
- # Print New Line on Complete
- if iteration == total:
- print()
- class Client:
- def __init__(self, host, port):
- self.host = host
- self.port = port
- def send(self, name: str, stream: io.BytesIO, size: int):
- BUFFER_SIZE = 1024
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
- s.connect((self.host, self.port))
- s.send(len(name).to_bytes(4, byteorder='little'))
- s.send(bytes(name, 'utf-8'))
- sent_num = 0
- while True:
- read_bytes = stream.read(BUFFER_SIZE)
- if not read_bytes: break
- r = s.send(read_bytes)
- sent_num += r
- printProgressBar(sent_num, size, "Sending the file: ", "", 1, 100)
- if __name__ == "__main__":
- FILENAME = sys.argv[1]
- HOST = sys.argv[2] if len(sys.argv) > 2 else '3.15.44.172'
- PORT = sys.argv[3] if len(sys.argv) > 3 else 65432
- with open(FILENAME, 'rb') as f:
- s = Client(HOST, PORT)
- s.send(FILENAME, f, os.stat(FILENAME).st_size)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement