我为大学助理实施了一个FTP服务器。我正在努力实现CWD功能。 路径传递给我的函数。这条路径可以是相对的和绝对的。我有一个包含当前工作目录的绝对路径的var。你能帮助我在传递的路径上做所有必要的合法性检查吗? 这是我到目前为止所编码的
def CWD(self, new_path):
if self.logged:
if new_path[0] != '/':
new_path = self.current_wd+'/'+new_path
if os.path.exists(new_path) and os.path.isdir(new_path) and self.users_dict[self.cur_user][1] in new_path:
self.current_wd = new_path
os.chdir(self.current_wd)
self.communicate_to_client('250 Requested file action okay, completed (relpath).')
else:
self.communicate_to_client('550 Requested action not taken (relpath).')
elif new_path in self.current_wd and self.users_dict[self.cur_user][1] in new_path:
self.current_wd = new_path
os.chdir(self.current_wd)
self.control_socket.send('250 Requested file action okay, completed.\r\n')
elif os.path.exists(new_path) and os.path.isdir(new_path) and self.users_dict[self.cur_user][1] in new_path:
self.current_wd = os.path.join(self.current_wd, new_path)
os.chdir(self.current_wd)
self.control_socket.send('250 Requested file action okay, completed.\r\n')
else:
self.control_socket.send('550 Requested action not taken in CWD.\r\n')
elif not self.logged and self.cur_user != '':
self.communicate_to_client('531 Insert password to take this operation')
else:
self.communicate_to_client('530 Authentication needed')