예제 #1
0
    def execute_command(self, args):
        response_addr = send_pasv(self.conn, self.encoding)

        if response_addr[0][0] != "2":
            print(response_addr)
            return

        ip_and_port = parse_from_response_to_ip_and_port(response_addr)

        self.conn.sendall(('list' + end_msg).encode(self.encoding))

        with open_connection_at(ip_and_port) as data_connection:
            response = read_response(self.conn, self.encoding,
                                     self.change_some_code)

            print(response)

            if response[0] != "1":
                print(response)
                return

            print(
                download_all_from(data_connection, None).decode(self.encoding))

        print(read_response(self.conn, self.encoding, self.change_some_code))
예제 #2
0
파일: cd.py 프로젝트: khorwl/ftp-client_2
    def execute_command(self, args):
        dir_name = " ".join(args)

        if args == "..":
            send_command(self.conn, "cdup" + end_msg, self.encoding,
                         self.change_some_code)
            print(
                read_response(self.conn, self.encoding, self.change_some_code))
            return

        send_command(self.conn, "cwd {}".format(dir_name) + end_msg,
                     self.encoding, self.change_some_code)
        print(read_response(self.conn, self.encoding, self.change_some_code))
예제 #3
0
    def execute_command(self, args):
        file_name = " ".join(args)

        send_command(self.conn, "dele {}".format(file_name) + end_msg,
                     self.encoding, self.change_some_code)

        print(read_response(self.conn, self.encoding, self.change_some_code))
예제 #4
0
    def execute_command(self, args):
        print("user:"******"user {}".format(user_name) + end_msg,
                     self.encoding, self.change_some_code)
        response = read_response(self.conn, self.encoding,
                                 self.change_some_code)

        if response[0] != "3":
            print(response)
            return

        password = getpass("Enter your password:"******"pass {}".format(password) + end_msg,
                     self.encoding, self.change_some_code)
        print(read_response(self.conn, self.encoding, self.change_some_code))
예제 #5
0
    def get_file_size(self):
        response = read_response(self.conn, self.encoding, self.change_some_code)[:-2].split()

        if response[0][0] == "2":
            return response[1]
        else:
            print(" ".join(response), end="\n\n")
            return None
예제 #6
0
    def execute_command(self, args):
        file_name = " ".join(args)
        ip_and_port = parse_from_response_to_ip_and_port(
            send_pasv(self.conn, self.encoding))

        send_command(self.conn, "stor " + file_name + end_msg, self.encoding,
                     self.change_some_code)

        with open_connection_at(ip_and_port) as data_connection:
            response = read_response(self.conn, self.encoding,
                                     self.change_some_code)

            if response[0] != "1":
                print(response)
                return

            send_all_by(data_connection, file_name)

        print(read_response(self.conn, self.encoding, self.change_some_code))
예제 #7
0
def run(addr, port, encoding, change_ff_code):
    with socket.socket() as s:
        s.connect((addr, port))

        print(read_response(s, encoding, change_ff_code))

        handler = Handler(s, encoding, change_ff_code)

        while True:
            print(">>>", end='')
            cmd = input()

            if cmd == '':
                print("You input empty string", end="\n\n")
                continue

            handler.execute_command_by_string(cmd)

            command_name = from_string_to_command_and_args(cmd)[0]

            if command_name == "quit":
                break
예제 #8
0
파일: pwd.py 프로젝트: khorwl/ftp-client_2
 def execute_command(self, args):
     self.conn.sendall(("pwd" + end_msg).encode(self.encoding))
     print(read_response(self.conn, self.encoding, self.change_some_code))
예제 #9
0
    def execute_command(self, args):
        file_name = " ".join(args)

        file_size = self.size_command.execute_command(file_name)

        if file_size is None:
            return

        mode = input("Which mode use? (active, passive)")

        if mode == 'passive':
            print('Executing at passive mode')
            ip_and_port = parse_from_response_to_ip_and_port(
                send_pasv(self.conn, self.encoding))

            send_command(self.conn, "retr {}".format(file_name) + end_msg,
                         self.encoding, self.change_some_code)

            with open_connection_at(ip_and_port) as data_connection:
                response = read_response(self.conn, self.encoding,
                                         self.change_some_code)

                if response[0] != "1":
                    print(response)
                    return

                write_bytes_to_file(
                    file_name,
                    download_all_from(data_connection, int(file_size)))

            print(
                read_response(self.conn, self.encoding, self.change_some_code))
        else:
            print('Executing at active mode')

            server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            server_socket.bind(("0.0.0.0", 0))
            server_socket.listen()
            ip = get_ip()
            port = int(server_socket.getsockname()[1]).to_bytes(
                2, byteorder='big')

            send_command(
                self.conn,
                f"port {','.join(ip.split('.'))},{port[0]},{port[1]}" +
                end_msg, self.encoding, self.change_some_code)
            response = read_response(self.conn, self.encoding,
                                     self.change_some_code)

            if response[0] != '2':
                print(response)
                return

            send_command(self.conn, "retr {}".format(file_name) + end_msg,
                         self.encoding, self.change_some_code)
            response = read_response(self.conn, self.encoding,
                                     self.change_some_code)

            if response[0] != '1':
                print(response)
                return

            con, addr = server_socket.accept()

            write_bytes_to_file(file_name,
                                download_all_from(con, int(file_size)))

            con.close()
            server_socket.close()

            print(
                read_response(self.conn, self.encoding, self.change_some_code))
예제 #10
0
def send_pasv(s, encoding):
    s.sendall(("pasv" + end_msg).encode(encoding))
    return read_response(s, encoding, False)