def handle(peer_socket): while True: try: line = Netutils.read_line(self.peer_socket) Auxiliaries.console_log("\nCommand IN: {} ".format(line)) if line is None: Auxiliaries.console_log("Client disconnecting ...") InPeer.incoming_peer_length = InPeer.incoming_peer_length - 1 break # Handling command line_parts = line.split() command = line_parts[0] req_args = [] if len(line_parts) > 1: req_args = line_parts[1:] # PING if command == "PING": response_to_send = self.ping_response(req_args) peer_socket.sendall(response_to_send) Auxiliaries.console_log( "\nCommand OUT: {} ".format(response_to_send)) # GET AVAILABLE BOOK elif command == "GET_AVAILABLE_BOOKS": response_to_send = self.get_available_books_response( req_args) peer_socket.sendall(response_to_send) Auxiliaries.console_log( "\nCommand OUT: {} ".format(response_to_send)) # REQUEST BOOK elif command == "REQUEST_BOOK": response_to_send = self.get_book_response(req_args) peer_socket.sendall( str.encode("{}\r\n".format(response_to_send))) # Auxiliaries.console_log("\nCommand OUT: {} ".format(response_to_send)) else: response_to_send = "400" peer_socket.sendall( str.encode("{}\r\n".format(response_to_send))) Auxiliaries.console_log( "\nCommand OUT: {} ".format(response_to_send)) except Exception as e: response_to_send = "500" peer_socket.sendall( str.encode("{}\r\n".format(response_to_send))) Auxiliaries.console_log( "\nCommand OUT: {} ".format(response_to_send)) Auxiliaries.console_log( "Exception occurred. disconnecting ...") break
def sock_read(self, timeout=None): """ ***************************************** Method used to read response from Candidate peer :return: String [UTF-8] ***************************************** """ return Netutils.read_line(self.tracker_socket)
def special_parsor(key, value): if key == "HUB_PORT" or key == "DISTRIBUTOR_PORT": if Auxiliaries.parse_port(value) is False: return False, "Invalid Port number provided range [0,65535]" if key == "HUB_IP": if Netutils.parse_ip_address(value) is False: return False, "'{}' does not appear to be an IPv4 or IPv6 address".format( value) return True, "Special parsor successfully completed"
def handle(): while True: try: line = Netutils.read_line(socket) Auxiliaries.console_log("\nCommand IN: {} ".format(line)) if line is None: Auxiliaries.console_log("Client disconnecting ...") break # Handling command line_parts = line.split() command = line_parts[0] # PING if command == "PING": response_to_send = "200" socket.sendall( str.encode("{}\r\n".format(response_to_send))) Auxiliaries.console_log( "\nCommand OUT: {} ".format(response_to_send)) # GET AVAILABLE BOOK elif command == "GET_AVAILABLE_BOOKS": response_to_send = "200 []" socket.sendall( str.encode("{}\r\n".format(response_to_send))) Auxiliaries.console_log( "\nCommand OUT: {} ".format(response_to_send)) # REQUEST BOOK elif command == "REQUEST_BOOK": response_to_send = "200 []" socket.sendall( str.encode("{}\r\n".format(response_to_send))) Auxiliaries.console_log( "\nCommand OUT: {} ".format(response_to_send)) else: response_to_send = "500" socket.sendall( str.encode("{}\r\n".format(response_to_send))) Auxiliaries.console_log( "\nCommand OUT: {} ".format(response_to_send)) except: response_to_send = "500" socket.sendall(str.encode("{}\r\n".format(response_to_send))) Auxiliaries.console_log( "\nCommand OUT: {} ".format(response_to_send)) Auxiliaries.console_log( "Exception occured. disconnecting ...") break
def get_ip(): # return "127.0.0.1" return Netutils.get_my_remote_ip()
def librarify(input_file, hub_address, chunk_size=BOOK_SIZE, output_path=None): """Takes a file, generates a .libr library file and returns its path. Params: ------ input_file -- a text or byte string giving the name of the file Should include the path if the file isn't in the current working directory. hub_address -- a string giving the address of the tracker and the port Should be in the following format: 'IPv4:PORT'. For example, '192.168.1.1:7777' is a valid hub address. chunk_size -- (int) size of each piece, in bytes. The input file will be split into chunks of the specified size. output_path -- (default None) is a path where libr file will be saved. If None, saves .libr in the same directory as input file. """ # ERROR CHECKING AND HANDLING # make sure input_file exists Librarifier._verify_input_file(input_file) # make sure hub address is OK (right type, is actually an IP:port) Librarifier._verify_hub_address(hub_address) # make sure output_path is ok, if provided if output_path: Librarifier._verify_output_path(output_path) data = {'hub_address': hub_address, 'chunk_size': chunk_size} hashes = [] with open(input_file, 'rb') as infile: while True: # read next chunk of bytes, quit if nothing left chunk_bytes = infile.read(chunk_size) if not chunk_bytes: break # compute hash value of each book (chunk) chunk_hash = Librarifier._bytes_to_sha1(chunk_bytes) # save these values hashes.append(chunk_hash) data['hashes'] = hashes # get file size file_size = os.path.getsize(input_file) # make sure we correctly split the file in chunks assert( len(hashes) == math.ceil(file_size/chunk_size) ) data['file_size'] = file_size # get filename with extension, save it, chop off extension input_file_directory, file_name = os.path.split(input_file) data['file_name'] = file_name pure_name, _ = os.path.splitext(file_name) # write meaningful information in library file if output_path is None: output_path = input_file_directory library_id = PREFIX + Netutils.get_timestamp() output_path = os.path.join(output_path,library_id + EXTENSION) with open(output_path, 'w') as outfile: json.dump(data, outfile, indent=4, sort_keys=True, separators=(',', ': ')) # OUTPUT: the path to the newly created .libr file if Path(output_path).exists() : return True, library_id, output_path else: return False,library_id, output_path