Exemplo n.º 1
0
 def handle(self, client, address, filename, content):
     if filename.endswith("/"):
         reply = make_response(1, "Invalid filename")
     elif filename.strip() == "":
         reply = make_response(1, "Invalid filename")
     else:
         with open(filename, "wb") as f:
             f.write(content)
         reply = make_response(0, "uploaded")
     send_data(client, address, reply)
Exemplo n.º 2
0
    def handle(self, client, address, command):
        print("Command:", command)
        # 解析args
        args = shlex.split(command)
        if len(args) == 0:
            reply = make_response(1, "invalid command")
            send_data(client, address, reply)
            return restrain_dir(args)

        # 命令名
        cmd = args[0]

        if not cmd in self.callback_table:
            reply = make_response(1, "Command '{}' doesn't exist.".format(cmd))
        else:
            code, stdout, stderr = self.callback_table[cmd](args)
            if code != 0:
                reply = make_response(code, stderr)
            else:
                reply = make_response(code, stdout)

        send_data(client, address, reply)

        return restrain_dir(args)
Exemplo n.º 3
0
    def handle(self, client, address, type_, data):

        data = data[FTPClientHandler.TYPE_SIZE + FTPClientHandler.CONTENT_SIZE: ]

        if type_ == FTPClientHandler.TYPE_COMMAND:
            # 返回解析过后的命令
            args = self.command_handler.handle(client, address, data.decode("UTF-8"))
            # 如果是上传任务的话
            if len(args) == 2 and args[0] == "put":
                self.filename = args[1]
                print("The file to be uploaded:", self.filename)
        elif type_ == FTPClientHandler.TYPE_FILE:
            print("The file to be uploaded:", self.filename)
            self.file_handler.handle(client, address, self.filename, data)
        else:
            print("unknown type:", type_)
            send_data(client, address, make_response(1, "unknown type:" + str(type_)))
        return