Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. #! /usr/bin/python3
  2. import os, sys, socket
  3.  
  4. class Sender:
  5. def __init__(self, path, file_name, ip_addr, port):
  6. self.path = path
  7. self.file_name = file_name
  8. self.ip_addr = ip_addr
  9. self.port = port
  10.  
  11. self.DEFAULT_SENDING_SIZE = 1024
  12.  
  13. def send(self):
  14. # change working directory to the place where the file is located
  15. self._chdir()
  16.  
  17. # open sockets to send a file
  18. self._open_socket()
  19.  
  20. # send size of the name and the name itself of the file that will be transferred
  21. file_name = self.file_name.encode()
  22. self._send_data(len(file_name).to_bytes(16, byteorder='big', signed=False))
  23. self._send_data(file_name)
  24.  
  25. # send size of the file that will be transferred
  26. file_size = os.stat(file_name).st_size
  27. self._send_data(file_size.to_bytes(64, byteorder='big', signed=False))
  28.  
  29. # send the file and count how much left
  30. sended_size = 0
  31. # Initial call to print 0% progress
  32. self._printProgressBar(sended_size, file_size, prefix='Progress:',
  33. suffix='Complete', length=50)
  34. ratio = sended_size / file_size
  35. while sended_size + self.DEFAULT_SENDING_SIZE < file_size:
  36. data = self._read_data(self.DEFAULT_SENDING_SIZE)
  37. self._send_data(data)
  38. sended_size += self.DEFAULT_SENDING_SIZE
  39. new_ratio = sended_size / file_size
  40. if new_ratio - ratio > 0.01:
  41. self._printProgressBar(sended_size, file_size, prefix='Progress:',
  42. suffix='Complete', length=50)
  43. ratio = new_ratio
  44.  
  45. # send last portion of the file
  46. data = self._read_data(file_size - sended_size)
  47. self._send_data(data)
  48. self._printProgressBar(sended_size, file_size, prefix='Progress:',
  49. suffix='Complete', length=50)
  50.  
  51. def _read_data(self, n):
  52. try:
  53. data = self.fd.read(n)
  54. except AttributeError:
  55. self.fd = open(self.file_name, 'rb')
  56. data = self.fd.read(n)
  57. return data
  58.  
  59. # the function was taken from StackOverflow and show progress bar
  60. def _printProgressBar(self, iteration, total, prefix='', suffix='', length=100, fill='█'):
  61. """
  62. Call in a loop to create terminal progress bar
  63. @params:
  64. iteration - Required : current iteration (Int)
  65. total - Required : total iterations (Int)
  66. prefix - Optional : prefix string (Str)
  67. suffix - Optional : suffix string (Str)
  68. length - Optional : character length of bar (Int)
  69. fill - Optional : bar fill character (Str)
  70. """
  71. percent = ("{:2.2f}".format(100 * (iteration / float(total))))
  72. filledLength = int(length * iteration // total)
  73. bar = fill * filledLength + '-' * (length - filledLength)
  74. print('\r{0} |{1}| {2}% {3}'.format(prefix, bar, percent, suffix), end='\r')
  75. print()
  76. # Print New Line on Complete
  77. if iteration == total:
  78. print()
  79.  
  80. def _send_data(self, data):
  81. self.sock.send(data)
  82.  
  83. def _open_socket(self):
  84. # AF_INET – IPv4, SOCK_STREAM – TCP
  85. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  86.  
  87. sock.connect((self.ip_addr, self.port))
  88. self.sock = sock
  89.  
  90. def _chdir(self):
  91. os.chdir(self.path)
  92.  
  93. def parse_path(path: str):
  94. delimeter = '/'
  95. # by default current directory is used
  96. dir = './'
  97. file = path
  98. if delimeter in path:
  99. index = path.rfind(delimeter)
  100. dir = path[:index+1]
  101. file = path[index+1:]
  102.  
  103. return dir, file
  104.  
  105. def main(argv):
  106. # ensure that all arguments are given
  107. if (len(argv) != 4):
  108. print("Invalid arguments. Try again")
  109. exit()
  110.  
  111. path, ip_addr, port = argv[1], argv[2], int(argv[3])
  112. dir, file_name = parse_path(path)
  113.  
  114. sender = Sender(dir, file_name, ip_addr, port)
  115. sender.send()
  116.  
  117. if __name__ == '__main__':
  118. main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement