コード例 #1
0
def main():
    my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    my_socket.connect(("127.0.0.1", 8888))

    print('Welcome to remote computer application. Available commands are:\n')
    print('TAKE_SCREENSHOT\nSEND_PHOTO\nDIR\nDELETE\nCOPY\nEXECUTE\nEXIT')
    # loop until user requested to exit
    while True:
        cmd = input("Please enter command:\n")
        try:
            if protocol.check_cmd(cmd):
                packet = protocol.create_msg(cmd)
                my_socket.send(packet.encode())
                length = my_socket.recv(4).decode()
                valid = protocol.get_msg(length)
                if valid:
                    serverResponse = my_socket.recv(int(length)).decode()
                    handle_server_response(my_socket, cmd, serverResponse)
                if serverResponse == "bay":
                    my_socket.close()
                    break
        except TypeError:
            print("invalid commend")
        except IndexError:
            print("invalid commend")
コード例 #2
0
def check_client_request(cmd):
    """
    Break cmd to command and parameters
    Check if the command and params are good.
    For example, the filename to be copied actually exists
    Returns:
        valid: True/False
        command: The requested cmd (ex. "DIR")
        params: List of the cmd params (ex. ["c:\\cyber"])
    """
    # Use protocol.check_cmd first
    valid_cmd = protocol.check_cmd(cmd)
    # Then make sure the params are valid
    if valid_cmd:
        list_cmd = cmd.split()
        print()
        if int(len(list_cmd)) == 1:
            return True, list_cmd[0], [None]
        elif list_cmd[0] == 'DIR':
            if os.path.isdir(list_cmd[1]):
                return True, 'DIR', [r'{}'.format(list_cmd[1])]
            else:
                print("Directory isn't exist")
                return False, "Error", ["Error"]
        elif list_cmd[0] == 'EXECUTE':
            if os.path.exists(list_cmd[1]):
                siyomet = list_cmd[1].split('.')
                print(siyomet)
                if siyomet[1].lower() == 'exe':
                    return True, 'EXECUTE', [r'{}'.format(list_cmd[1])]
                else:
                    print("File isn't execute file")
                    return False, "Error", ["Error"]
            else:
                print("File isn't exist")
                return False, "Error", ["Error"]
        elif list_cmd[0] == 'SEND_PHOTO' or list_cmd[0] == 'DELETE':
            if os.path.isfile(list_cmd[1]):
                return True, list_cmd[0], [r'{}'.format(list_cmd[1])]
            else:
                print("File isn't exist")
                return False, "Error", ["Error"]
        elif list_cmd[0] == 'COPY':
            if os.path.isfile(list_cmd[1]):
                find_last_slash = list_cmd[2].rfind('\\')
                dir_file = list_cmd[2][0:find_last_slash:]
                print(dir_file)
                if os.path.isdir(dir_file):
                    return True, 'COPY', [
                        r'{}'.format(list_cmd[1]), r'{}'.format(list_cmd[2])
                    ]
                else:
                    print("Directory isn't exist")
                    return False, "Error", ["Error"]
    else:
        return False, "Error", ["Error"]
コード例 #3
0
def main():
    my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # open socket with the server
    my_socket.connect((IP, protocol.PORT))
    # print instructions
    print('Welcome to remote computer application. Available commands are:\n')
    print('TAKE_SCREENSHOT\nSEND_PHOTO\nDIR\nDELETE\nCOPY\nEXECUTE\nEXIT')
    # loop until user requested to exit
    while True:
        cmd = input("Please enter command:\n")
        if protocol.check_cmd(cmd):
            packet = protocol.create_msg(cmd)
            my_socket.send(packet)
            handle_server_response(my_socket, cmd)
            if cmd == 'EXIT':
                break
        else:
            if cmd == 'EXIT':
                break
            print("Not a valid command, or missing parameters\n")

    print("Closing\n")
    my_socket.close()  # Close socket
コード例 #4
0
def check_client_request(cmd):
    """
    Break cmd to command and parameters
    Check if the command and params are good.

    For example, the filename to be copied actually exists

    Returns:
        valid: True/False
        command: The requested cmd (ex. "DIR")
        params: List of the cmd params (ex. ["c:\\cyber"])
    """
    # Use protocol.check_cmd first
    if protocol.check_cmd(cmd):
        command = cmd.split(" ")
        params = []
        is_valid = True
        if len(command) == 2:
            path = command[1]
            if os.path.exists(path):
                params.append(path)
            else:
                is_valid = False

        elif len(command) == 3:
            path_from = command[1]
            path_to = command[2]
            if os.path.exists(path_from):
                params.append(path_from)
                params.append(path_to)
            else:
                is_valid = False

    else:
        is_valid = False

    return is_valid, command[0], params