Advertisement
Guest User

client.py

a guest
Nov 15th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. import mimetypes
  2.  
  3. from network import Network
  4.  
  5.  
  6. def is_binary(file):
  7.     try:
  8.         with open(file, 'r') as f:
  9.             f.read()
  10.         return False
  11.     except UnicodeDecodeError:
  12.         f.close()
  13.         return True
  14.  
  15.  
  16. network = Network()
  17. command = input("Type in a command: ")
  18. if command == "send file: ":
  19.     file = command.split(": ")[1]
  20.     network.send("file start")
  21.     print("run")
  22.  
  23.     datadict = dict()
  24.     datadict["name"] = file
  25.     datadict["encoding"] = mimetypes.guess_type(file)[1]
  26.     datadict["binary"] = is_binary(file)
  27.  
  28.     block_count = 0
  29.     blocks = list()
  30.  
  31.     with open(file, 'r' + 'b' if datadict["binary"] else '') as f:
  32.         l = f.read(2048)
  33.         while l:
  34.             print("done")
  35.             block_count += 1
  36.             blocks.append(l)
  37.             l = f.read(2048)
  38.  
  39.     datadict["blocks"] = block_count
  40.     print(datadict)
  41.     for b in blocks:
  42.         network.send(b)
  43.  
  44.     if network.receive() != 1:
  45.         print("Error in transmitting file.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement