def get_request_server(word_list, s, token_str): #GETリクエスト filename = word_list[1] print(token_str) if word_list[2] == pbl2017.genkey(token_str): #トークンが一致した場合 if word_list[3] == 'ALL': #ALL try: f = open(filename) file_size = os.path.getsize(filename) sentence = 'OK Sending {} from 0 to {} total {} bytes at {}\n'.format( filename, file_size - 1, file_size, time.time()) s.send(sentence.encode()) #print(sentence) tmp_data = f.read() s.send(tmp_data.encode()) except: print('NG 101 NO such file') s.send('NG 101 NO such file\n'.encode()) elif word_list[3] == 'PARTIAL': #PARTIAL try: f = open(filename) offset = int(word_list[4]) length = int(word_list[5]) sentence = 'OK Sending {} from {} to {} total {} bytes\n'.format( filename, offset, length, length - offset + 1) s.send(sentence.encode()) print(sentence) f.seek(offset) tmp_data = f.read(length) s.send(tmp_data.encode()) except: print('NG 101 NO such file') s.send('NG 101 NO such file\n'.encode())
def interact_with_client(s): token_str = "aaa" print("hash:", pbl2017.genkey(token_str)) #sentence = s.recv(1024).decode()#1回目のclientからのデータ受信SIZE,GET,REP... tmp_sentence = receive_data2(s) sentence = tmp_sentence print('受信:', end="") word_list = sentence.split() if len(word_list) == 0: #word_listが何もなし print('Invalid_request') s.send('NG 301 Invalid command\n'.encode()) elif word_list[0] == 'SIZE': #SIZE print('SIZE_request') size_request_server(word_list, s) elif word_list[0] == 'GET': #GET print('GET_request') get_request_server(word_list, s, token_str) elif word_list[0] == 'REP': #REP print('REP_request') rep_request_server(word_list, s, token_str) else: print('Invalid_request') s.send('NG 301 Invalid command\n'.encode()) s.close()
def GET_FILE_request(arg_str, client_socket): filename = arg_str[3] token_str = arg_str[4] print('token_str:', arg_str[4]) getarg = pbl2017.genkey(token_str) print("hash:", getarg) sentence = '{} {} {} {} \n'.format("GETFILE", filename, getarg, int(arg_str[2])) #GETFILE rnd50K.txt toke_nstr server_port client_socket.send(sentence.encode()) res_str = receive_data(client_socket) #データを受信 print(res_str)
def get_request_client(input_list, client_socket, token_str): #GETリクエスト getarg = pbl2017.genkey(token_str) #トークン文字列から生成したダイジェスト文字列を代入 if (input_list[2] == 'ALL'): #ALL filename = input_list[1] sentence = 'GET {} {} {}\n'.format( input_list[1], getarg, 'ALL') #GET filename token ALL/PARTIAL sNUM gNUM print("[TO server]\n" + sentence) client_socket.send(sentence.encode()) #サーバーへリクエスト res_str = receive_data2(client_socket) #サーバーからの応答を受信 #res_str = client_socket.recv(1024).decode() #res_str = client_socket.recv(1024).decode() print('[FROM server]\n' + res_str) if (res_str.split()[0] == 'OK'): #OK ALL_file_data = receive_data(client_socket) #ファイルデータ受信 f = open(filename, 'w') f.write(ALL_file_data) f.close() #print(ALL_file_data) elif (res_str.split()[0] == 'NG'): #NG print(res_str) elif (input_list[2] == 'PARTIAL'): #PARTIAL sentence = 'GET {0} {1} PARTIAL {2} {3}\n'.format( input_list[1], getarg, input_list[3], input_list[4]) print("[TO server]\n" + sentence) client_socket.send(sentence.encode()) #サーバーへリクエスト res_str = receive_data(client_socket) #サーバーからの応答を受信 #res_str = client_socket.recv(1024).decode() print('[FROM server]\n' + res_str) if (res_str.split()[0] == 'OK'): PARTIAL_file_data = receive_data(client_socket) print(PARTIAL_file_data) print(res_str) elif (res_str.split()[0] == 'NG'): print(res_str)