def client(ip, port): # Contacts the metadata server sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((ip, port)) # Ask for list of files p = Packet() p.BuildListPacket() sock.send(p.getEncodedPacket()) # Receive msg from MDS msg = sock.recv(LS_BUFFER) if msg == "NAK": print "Error ocurred when receiving `ls` response! Exiting..." sock.close() sys.exit(0) # Decode msg p.DecodePacket(msg) # Get list of (fname, fsize) flist = p.getFileArray() # Print fname followed by fsize for n, s in flist: print "%s %d bytes" % (n, s) # Close connection sock.close()
def client(ip, port): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((ip, port)) p = Packet() p.BuildListPacket() msg = p.getEncodedPacket() sock.sendall(msg.encode()) msg = sock.recv(1024).decode() p.DecodePacket(msg) print(msg) sock.close() # cierra coneccion
def client(ip, port): # Contacts the metadata server and ask for list of files. p = Packet() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((ip, port)) p.BuildListPacket() # build list request package sock.sendall(p.getEncodedPacket() ) # package must be encoded before sending throught the net data = sock.recv(1024) # recieve data sent from meta-data server p.DecodePacket(data) # recieved data must be decoded before reading files = p.getFileArray() # get array of files after packet decoded for file in files: print os.path.abspath(file[0]), file[1], "bytes"
def client(ip, port): # Contacts the metadata server and ask for list of files. p = Packet() p.BuildListPacket() packet = p.getEncodedPacket() # convert packet to bytes s = socket.socket() s.connect((ip, port)) s.sendto(packet, (ip, port)) # sending packet to server # go to server packetReceived = s.recvfrom(1024) #receiving packet from server and it is a tuple p.DecodePacket(packetReceived[0]) # converting packet from bytes to its original type, packetReceived[0] is where the byte packet is packet = p.packet files = packet['files'] for file in files: print(file[0], file[1])
def client(ip, port): # Contacts the metadata server and ask for list of files. socko = socket.socket(socket.AF_INET, socket.SOCK_STREAM) socko.connect((ip, port)) packet = Packet() packet.BuildListPacket() data_message = packet.getEncodedPacket() socko.sendall(data_message) rec = socko.recv(4096) dec_packet = Packet() dec_packet.DecodePacket(rec) print_files = dec_packet.getFileArray() socko.close() for n, s in print_files: print """%s %d bytes""" % (n, s)
def client(ip, port): # Contacts the metadata server and ask for list of files. so = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: #print("Here") #print("connected") #Creating a packet that will request the list to the meta-data server. packet_list_request = Packet() #print("Packet request built") #Using the function BuildListPacket() from Packet.py, #the packet will have the list command inside and it will let know #the meta-data server how to handle the request. packet_list_request.BuildListPacket() list_request = packet_list_request.getEncodedPacket() #Connecting to the socket . so.connect((ip, port)) #sending list request packet to meta-data server. so.sendall(list_request) #print packet_list_request #print("receiving list from meta-data server") #list_response will contain the response from the meta-data server to the #list request. list_response = so.recv(4096) #create a packet to decode and have access to the list that was sent by meta-data.py. packet_list = Packet() packet_list.DecodePacket(list_response) #closing socket connection. so.close() #printing the file list. for file, size in packet_list.getFileArray(): print """%s %d bytes""" % (file, size) except: raise
print("Connection with the server failed. Error: " + str(e) ) #error if connection fails sys.exit(1) print("Connection to the MetaData Server was successful." ) #if it doesn't fail, the connection was succesful! # We have this try where it will continue checking till the reply is not equal to NAK. # When that occurs it will check if there are any existing files and print them out. # If there are no files then it will say so and exit the while if eitehr of these happen. try: packet = Packet() reply = "NAK" while reply == "NAK": packet.BuildListPacket() created_socket.sendall(packet.getEncodedPacket()) reply = created_socket.recv(1024) if reply != "NAK": packet.DecodePacket(reply) file_list = packet.getFileArray() if not file_list: print "No files exists here." else: for a in file_list: print(a[0], str(a[1]) + "bytes") finally: created_socket.close() #socket that was used is closed