def run(self): #create dropbox opject box = dropbox.dropbox('test') #start listening loop while True: data = None #recieve some data request = self.socket.recv(1024) #print request print request #split data on the spaces data = request.split() content = request.split("\r\n\r\n") #first value of split data is the command if data[0]: #send a directory listing to client if data[0].lower() == 'dir': self.socket.send(box.directory_listing()) #attempt to authenticate a user elif data[0].lower() == 'login': login_info = data[3].split(':') self.socket.send(box.authenticate(login_info[0], login_info[1])) #request to exit the program elif data[0].lower() == 'exit': self.socket.send(box.response(data[0], 200)) break #request to choose summary selection elif data[0].lower() == 'exg': response = box.summary_selection(request) self.socket.send(response) #request to download a file from the server elif data[0].lower() == 'get': response = box.response(data[0], 200) response = response + box.send_file(str(data[1])) self.socket.send(response) #sending a file to the server elif data[0].lower() == 'put': self.socket.send(box.response(data[0], 200)) box.save_file(content[1], data[1]) #recieve a request to delete a file elif data[0].lower() == 'delete': self.socket.send(box.response(data[0], 200)) box.delete_file(data[1]) print 'trying to delete something.' #in the event of a command not being understood, an error is sent to the client else: self.socket.send(box.response(data[0], 400)) #close the socket when communication complete self.socket.close()
def main(args): command = sys.argv[1:][0] result = [] if command == "list": result = command_list(args) elif command == "reg": result = reg(args) else: cloud = dropbox(args) commands = { "dir": cloud.dir, "download": cloud.download, "upload": cloud.upload} result = commands[command]() return result
# arg[2] = host name # arg[3] = username # arg[4] = password if len(args) != 5: print "Must give the following information:" print "-d directory -p port# -h host_name -u username -q password" sys.exit(0) try: int(args[1]) except ValueError: print "The -p value " + args[1] + " is not an integer." sys.exit(0) size = 4096 # Create the socket connection box = dropbox.dropbox(args[0]) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect((args[2], int(args[1]))) # sock.connect(('euro.cs.dal.ca', 22345)) except: print "Server not responding, exiting..." sys.exit(0) sock.send(box.login(args[3], args[4])) # sock.send(box.login('bob', 'password')) login_response = sock.recv(size) print login_response response_code = login_response.split() if response_code[2] == "401" or response_code[2] == "402": sock.send(box.request("EXIT")) sys.exit(0)
import socket import dropbox backlog = 2 box = dropbox.dropbox('test') size = 1024 serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversock.bind(('euro.cs.dal.ca',22345)) serversock.listen(backlog) clientsock, addr = serversock.accept()#need threads to accept more than one connection. while True: data = None request = clientsock.recv(size) print request data = request.split() if data[0]: if data[0] == 'DIR': clientsock.send(box.directory_listing()) elif data[0] == 'Login': login_info = data[3].split(':') clientsock.send(box.authenticate(login_info[0], login_info[1])) elif data[0] == 'EXIT': clientsock.send(box.response(data[0], 200)) break else: clientsock.send(box.response(data[0], 400)) clientsock.close() serversock.close()