Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/python3
- import os, sys, socket
- class Sender:
- def __init__(self, path, file_name, ip_addr, port):
- self.path = path
- self.file_name = file_name
- self.ip_addr = ip_addr
- self.port = port
- self.DEFAULT_SENDING_SIZE = 1024
- def send(self):
- # change working directory to the place where the file is located
- self._chdir()
- # open sockets to send a file
- self._open_socket()
- # send size of the name and the name itself of the file that will be transferred
- file_name = self.file_name.encode()
- self._send_data(len(file_name).to_bytes(16, byteorder='big', signed=False))
- self._send_data(file_name)
- # send size of the file that will be transferred
- file_size = os.stat(file_name).st_size
- self._send_data(file_size.to_bytes(64, byteorder='big', signed=False))
- # send the file and count how much left
- sended_size = 0
- # Initial call to print 0% progress
- self._printProgressBar(sended_size, file_size, prefix='Progress:',
- suffix='Complete', length=50)
- ratio = sended_size / file_size
- while sended_size + self.DEFAULT_SENDING_SIZE < file_size:
- data = self._read_data(self.DEFAULT_SENDING_SIZE)
- self._send_data(data)
- sended_size += self.DEFAULT_SENDING_SIZE
- new_ratio = sended_size / file_size
- if new_ratio - ratio > 0.01:
- self._printProgressBar(sended_size, file_size, prefix='Progress:',
- suffix='Complete', length=50)
- ratio = new_ratio
- # send last portion of the file
- data = self._read_data(file_size - sended_size)
- self._send_data(data)
- self._printProgressBar(sended_size, file_size, prefix='Progress:',
- suffix='Complete', length=50)
- def _read_data(self, n):
- try:
- data = self.fd.read(n)
- except AttributeError:
- self.fd = open(self.file_name, 'rb')
- data = self.fd.read(n)
- return data
- # the function was taken from StackOverflow and show progress bar
- def _printProgressBar(self, iteration, total, prefix='', suffix='', 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)
- length - Optional : character length of bar (Int)
- fill - Optional : bar fill character (Str)
- """
- percent = ("{:2.2f}".format(100 * (iteration / float(total))))
- filledLength = int(length * iteration // total)
- bar = fill * filledLength + '-' * (length - filledLength)
- print('\r{0} |{1}| {2}% {3}'.format(prefix, bar, percent, suffix), end='\r')
- print()
- # Print New Line on Complete
- if iteration == total:
- print()
- def _send_data(self, data):
- self.sock.send(data)
- def _open_socket(self):
- # AF_INET – IPv4, SOCK_STREAM – TCP
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.connect((self.ip_addr, self.port))
- self.sock = sock
- def _chdir(self):
- os.chdir(self.path)
- def parse_path(path: str):
- delimeter = '/'
- # by default current directory is used
- dir = './'
- file = path
- if delimeter in path:
- index = path.rfind(delimeter)
- dir = path[:index+1]
- file = path[index+1:]
- return dir, file
- def main(argv):
- # ensure that all arguments are given
- if (len(argv) != 4):
- print("Invalid arguments. Try again")
- exit()
- path, ip_addr, port = argv[1], argv[2], int(argv[3])
- dir, file_name = parse_path(path)
- sender = Sender(dir, file_name, ip_addr, port)
- sender.send()
- if __name__ == '__main__':
- main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement