def copyFromDFS(address, fname, path): """ Contact the metadata server to ask for the file blocks of the file fname. Get the data blocks from the data nodes. Saves the data in path. """ # Contact the metadata server to ask for information of fname # Create socket and connect to metadata server metaSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) metaSock.connect(address) # Create get packet and send request to metadata server p = Packet() p.BuildGetPacket(fname) metaSock.sendall(p.getEncodedPacket()) # Recieve chunk list msg = b"" while True: buffer = metaSock.recv(2048) msg += buffer if getsizeof(buffer) < 2048: break # If file not found, exit, if file found, decode packet if msg != b"NFOUND": p.DecodePacket(msg) else: exit("File not found, exiting...") # If there is no error response Retreive the data blocks # Get blocks blocks = p.getDataNodes() # Open file fd = open(path, "wb+") for ip, addr, blockId in blocks: # Create socket and connect to node nodeSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) nodeSock.connect((ip, addr)) # Build packet and send request to data node p.BuildGetDataBlockPacket(blockId) nodeSock.send(p.getEncodedPacket()) # Recieve block and write to file named path fd.write(nodeSock.recv(16384)) # Close socket nodeSock.close()