def do_put(self,line): """Puts the file into the server filename : The filename should be in same folder. encflag : "E" or "N", whether encryption is required or not opt<password> : Password<8 Characters> for encrypting the file. """ args = line.split(" ") if len(args)==2: #case put <filename> <encflag> filename, encflag = line.split(" ") if not os.path.isfile(filename): print ("Error: %s cannot be transferred" %filename) self.cmdloop() if encflag!='N': print "Wrong parameter" self.cmdloop() elif len(args)==3: #case put <filename> <encflag> <password> filename ,encflag, password = line.split(" ") if not os.path.isfile(filename): print "Error: File not found. Should be in same folder!" self.cmdloop() if encflag!="E" or len(password)!=8: print "Error: Wrong Flag/password" self.cmdloop() else: print "Error: Wrong Number of Arguments!!" self.cmdloop() fhash = Common.gen_hash(filename) # Hash generated send it now! if encflag=="E": Common.encrypt_file(password,filename) with open(filename+".enc") as f: msg = f.read() os.remove(filename+".enc") else: with open(filename) as f: msg = f.read() Common.send_msg(self.clientsocket,"put") #Set mode to put Common.send_msg(self.clientsocket,msg) Common.send_msg(self.clientsocket,fhash) Common.send_msg(self.clientsocket,filename) #Wait for Response from Server print Common.recv_msg(self.clientsocket)
def do_get(self,line): """ Gets the file from the server!! line: filename : The filename to be retrieved. <encflag> : "E" or "N", whether the file was encrypted. <opt password> : Password<8 Characters> for decrypting the file. """ args = line.split(" ") if len(args)==2: #case get <filename> <encflag = N> filename, encflag = line.split(" ") if encflag!='N': print "Error: Wrong Flag" self.cmdloop() elif len(args)==3: #case get <filename> <encflag = E> <password> filename, encflag, password = line.split(" ") if len(password)!=8: print "Password is short <8 Characters>" self.cmdloop() if encflag!='E': print "Wrong Flag" self.cmdloop() else: print "Wrong Number of Arguments" self.cmdloop() Common.send_msg(self.clientsocket,"get") Common.send_msg(self.clientsocket, filename) status = Common.recv_msg(self.clientsocket) if status=="OK": data = Common.recv_msg(self.clientsocket) fhash = Common.recv_msg(self.clientsocket) with open('tmp_client/'+filename+".enc","w") as f: f.write(data) with open('tmp_client/'+filename+".sha256","w") as f: f.write(fhash) fname = 'tmp_client/'+filename+".enc" if encflag=='E': #Client assumes the file was encrypted. if not Common.decrypt_file(password, fname): #File was not encrypted to begin with!! print ("Error: decryption of %s failed, was file encrypted?" %filename) os.remove('tmp_client/'+filename+".sha256") # sha of file os.remove('tmp_client/'+filename+".enc") #enc file else: #File decrypted check hash filehash = Common.gen_hash('tmp_client/'+filename) if fhash==filehash: print "retrieval of %s complete" %filename else: print ("Error: Computed hash of %s does not match " "retrieved hash" %filename) os.remove('tmp_client/'+filename) #Irrespecive of Match or not delete the hashed file. os.remove('tmp_client/'+filename+".sha256") os.remove('tmp_client/'+filename+".enc") else: #Client assumes no encryption was applied filehash = Common.gen_hash('tmp_client/'+filename+".enc") if fhash==filehash: print "retrieval of %s complete "%filename else: print ("Error: Computed hash of %s does not match " "retrieved hash" %filename) with open('tmp_client/'+filename,"w") as f: f.write(data) os.remove('tmp_client/'+filename+".sha256") os.remove('tmp_client/'+filename+".enc") else: #Server Error Occured. print status