def send_file(self, file_name):
     hash_list_to_send = []
     data_map = {}
     if os.path.exists(file_name):
         try:
             file = open(file_name, 'rb')
             hash_file = utilities.md5_for_file(file_name)
             print("hash_file to send: " + hash_file + "(" + file_name +
                   ")")
             # print("chunk file: "+str(utilities.chunk_file))
             part = 0
             while True:
                 data = file.read(utilities.chunk_file)
                 # print(data)
                 if not data:
                     print("no more data")
                     break
                 hash_subpart = utilities.md5_for_data(data)
                 hash_subpart = hash_subpart + ".part" + str(part)
                 hash_list_to_send.append(hash_subpart)
                 data_map[hash_subpart] = data
                 part += 1
             print("sending the hashes")
             self.socket_to_balancer.send_multipart([
                 'servers_to_send_files'.encode(),
                 str(hash_file).encode(),
                 str(hash_list_to_send).encode()
             ])
             response = self.socket_to_balancer.recv_multipart()
             # print(response)
             status = response[0].decode()
             if status == 'OK':
                 files_list = json.loads(response[1].decode())
                 servers_address = list(files_list.keys())
                 for count in range(len(servers_address)):
                     address = servers_address[count]
                     # print(files_list[address])
                     # print(type(files_list[address]))
                     list_of_hashes = files_list[address]
                     context_to_server = zmq.Context()
                     socket_to_server = context_to_server.socket(zmq.REQ)
                     socket_to_server.connect(address)
                     for hash_file in list_of_hashes:
                         data = data_map[hash_file]
                         # print("socket_to_server, "+address+", "+hash_file)
                         # print(type(data))
                         socket_to_server.send_multipart([
                             'send_file'.encode(),
                             hash_file.encode(), data
                         ])
                         message = socket_to_server.recv_multipart()
                         # print(message)
             else:
                 message = response[1].decode()
                 print("ERROR - " + message)
             print("finish to send the file")
         except IOError as error:
             print(error)
     else:
         print('The file "' + file_name + '" no exist. Try again')
Exemplo n.º 2
0
 def send_file(self, file_name):
     if os.path.exists(file_name):
         try:
             file = open(file_name, 'rb')
             hash_file = utilities.md5_for_file(file_name)
             print("hash_file to send: " + hash_file + "(" + file_name +
                   ")")
             # print("chunk file: "+str(self.chunk_file))
             part = 0
             while True:
                 data = file.read(self.chunk_file)
                 # print(data)
                 if not data:
                     print("no more data")
                     print("data: " + str(part * self.chunk_file))
                     part = -1
                     self.socket.send_multipart([
                         'send_file'.encode(),
                         str(hash_file).encode(),
                         str(part).encode(), ''.encode()
                     ])
                     print("server response: " +
                           self.socket.recv_multipart()[0].decode())
                     break
                 print("sending the part: " + str(part))
                 self.socket.send_multipart([
                     'send_file'.encode(),
                     str(hash_file).encode(),
                     str(part).encode(), data
                 ])
                 server_response = self.socket.recv_multipart()
                 print("server response: " + server_response[0].decode())
                 part += 1
                 if server_response[0].decode() != 'OK':
                     print("ERROR - " + server_response[1].decode())
                     break
             print("finish to send the file")
         except IOError as error:
             print(error)
     else:
         print('The file "' + file_name + '" no exist. Try again')
Exemplo n.º 3
0
                    self.dictionary_files[hash_file]["parts"].pop(count)
                    count = 0
                else:
                    count += 1


if __name__ == '__main__':
    client = Client()
    while True:
        print("write the file name to send: ")
        file_name1 = input()
        if file_name1 != '':
            client.send_file(file_name1)
            hash_file1 = ''
            if os.path.exists(file_name1):
                hash_file1 = utilities.md5_for_file(file_name1)
            print("press any key for continue to get the same file: ")
            input()
            client.get_file(hash_file1, file_name1)
        else:
            print("The file name to send can not be empty. Try again")
        # print("press any key to get the file: 9694b7c165af81f4d9d372f54117c0f1")
        # input()
        # client.get_file_by_hash('9694b7c165af81f4d9d372f54117c0f1')
        # print("press any key to send server file:")
        # input()
        # client.send_file('server.py')
        # print("press any key to send client file:")
        # input()
        # client.send_file('client.py')
        # print("press any key to send utilities file:")
Exemplo n.º 4
0
 def send_file(self, file_name):
     hash_list_to_send = []
     data_map = {}
     if os.path.exists(file_name):
         try:
             file = open(file_name, 'rb')
             hash_file = utilities.md5_for_file(file_name)
             print("hash_file to send: " + hash_file + "(" + file_name +
                   ")")
             # print("chunk file: "+str(utilities.chunk_file))
             part = 0
             while True:
                 data = file.read(self.chunk_file)
                 # print(data)
                 if not data:
                     print("no more data")
                     break
                 hash_subpart = utilities.md5_for_data(data)
                 hash_subpart = hash_subpart + ".part" + str(part)
                 hash_list_to_send.append(hash_subpart)
                 data_map[hash_subpart] = data
                 part += 1
             print("sending the sub parts")
             address_to_server = self.server_ip
             self.dictionary_files[hash_file] = []
             for count in range(len(hash_list_to_send)):
                 print("Sending the part: " + hash_list_to_send[count])
                 while True:
                     context_to_server = zmq.Context()
                     socket_to_server = context_to_server.socket(zmq.REQ)
                     socket_to_server.connect("tcp://" + address_to_server)
                     print("connect to: " + address_to_server +
                           " and ask is your hash " +
                           hash_list_to_send[count])
                     socket_to_server.send_multipart([
                         'is_your_hash'.encode(),
                         hash_list_to_send[count].split(
                             ".part")[0].encode()
                     ])
                     message = socket_to_server.recv_multipart()
                     if message[0].decode() == 'yes':
                         print("Server response: YES")
                         self.dictionary_files[hash_file].append({
                             "address":
                             address_to_server,
                             "part":
                             hash_list_to_send[count]
                         })
                         socket_to_server.send_multipart([
                             'send_file'.encode(),
                             hash_list_to_send[count].encode(),
                             data_map[hash_list_to_send[count]]
                         ])
                         message = socket_to_server.recv_multipart()
                         break
                     else:
                         print("Server response: NO")
                         address_to_server = message[1].decode()
             print("finish to send the file")
         except IOError as error:
             print(error)
     else:
         print('The file "' + file_name + '" no exist. Try again')