def onResponse(message): ip_address = socket.gethostbyname(socket.gethostname()) for url in message: if download(url): node_url = 'http://%s:11009/%s' % (ip_address, sys.argv[1]) # 'set' the file in our location (node_url) -> we are now registered as a server for it ipcSend('set %s %s' % (sys.argv[1], node_url)) return print('File not downloaded.')
def onResponse(message): # we get our ip_adress by accessing the socket ip_address = socket.gethostbyname(socket.gethostname()) for url in message: # as message is a list of potential places in which the file is located, we iterate over them if download(url): # we try to download the url and if we succeed, we register ourselves. node_url = 'http://%s:11009/%s' % (ip_address, sys.argv[1]) # 'set' the file in our location (node_url) -> we are now registered as a server for it ipcSend('set %s %s' % (sys.argv[1], node_url)) print( 'Downloaded %s succesfully and saved into %s/. Serving the file.' % (sys.argv[1], FILES_FOLDER)) return print('File not downloaded. The following errors ocurred:') # only if the file wasn't downloaded we print the errors. print(*('%d) %s' % (i + 1, ERRORS[i]) for i in range(len(ERRORS))), sep='\n')
from kademlia_iic2523.ipc import ipcSend from shutil import copyfile import socket import sys import os FILES_FOLDER = '/user/rjherrera/T2/files' # setting variables (file_name to publish, ip of the host, (i.e. us) to get the url, url to publish) file_name = os.path.basename(sys.argv[1]) ip_address = socket.gethostbyname(socket.gethostname()) url = 'http://%s:11009/%s' % (ip_address, file_name) # 'copy' the file into the serving location -> we are now serving it as we have a running server on that location copyfile(sys.argv[1], '%s/%s' % (FILES_FOLDER, file_name)) # 'set' the file in our location (url) -> we are now registered in the DHT as a server for it ipcSend('set %s %s' % (file_name, url)) print('Uploaded %s succesfully.' % file_name)
# we throw an exception if the file wasn't located (if the status code isn't 200) raise Exception(r.status_code) except Exception as error: # we append the errors to a list so that they can be printed only if needed ERRORS.append(error) return False def onResponse(message): # we get our ip_adress by accessing the socket ip_address = socket.gethostbyname(socket.gethostname()) for url in message: # as message is a list of potential places in which the file is located, we iterate over them if download(url): # we try to download the url and if we succeed, we register ourselves. node_url = 'http://%s:11009/%s' % (ip_address, sys.argv[1]) # 'set' the file in our location (node_url) -> we are now registered as a server for it ipcSend('set %s %s' % (sys.argv[1], node_url)) print( 'Downloaded %s succesfully and saved into %s/. Serving the file.' % (sys.argv[1], FILES_FOLDER)) return print('File not downloaded. The following errors ocurred:') # only if the file wasn't downloaded we print the errors. print(*('%d) %s' % (i + 1, ERRORS[i]) for i in range(len(ERRORS))), sep='\n') # we use the get command and register as a callback the above function ipcSend('get %s' % sys.argv[1], onResponse)
from kademlia_iic2523.ipc import ipcSend import sys # Callback que se ejecutará con la respuesta del nodo def onResponse(message): print(message) # Envía el comando "get key" al nodo ipcSend("get " + sys.argv[1], onResponse)
from kademlia_iic2523.ipc import ipcSend import sys # Envía el string "set key value" al nodo ipcSend("set " + sys.argv[1] + " " + sys.argv[2])