Пример #1
0
def command_cd(client, args):
    tlv = tls.TLV(tls.REQ_W_DIR_CHANGE, args.param)
    print(args.param)
    client.send_tlv(tlv)
    tlv = client.recv_tlv()
    if tlv.tag == tls.RES_ERROR:
        print('error: {}'.format(tlv.val))
Пример #2
0
def command_lst(client, args):
    tlv = tls.TLV(tls.REQ_DIR)
    client.send_tlv(tlv)
    tlv = client.recv_tlv()
    if tlv.tag == tls.RES_ERROR:
        print('error:{}'.format(tlv.val))
    elif tlv.tag == tls.RES_DIR:
        print(tlv.val)
Пример #3
0
def command_pwd(client, args):
    tlv = tls.TLV(tls.REQ_W_DIR)
    client.send_tlv(tlv)
    tlv = client.recv_tlv()
    if tlv.tag == tls.RES_ERROR:
        print('error: {}'.format(tlv.val))
    elif tlv.tag == tls.RES_CMD_OK:
        print('remote:{0}'.format(tlv.val))
Пример #4
0
def command_get(client, args):
    tlv = tls.TLV(tls.REQ_FILE, args.param)
    client.send_tlv(tlv)
    tlv = client.recv_tlv()
    if tlv.tag == tls.RES_ERROR:
        print('Error:{}'.format(tlv.val))
    elif tlv.tag == tls.RES_FILE_BEG:
        w_dir = client.working_dir
        file_name = tlv.val
        file_abs_name = os.path.join(w_dir, file_name)
        file_object = open(file_abs_name, 'wb')
        tlv = client.recv_tlv()
        if tlv.tag == tls.RES_FILE_SIZE:
            eval(tlv.val)
        tlv = client.recv_tlv()
        while tlv.tag != tls.RES_FILE_END:
            file_object.write(tlv.val)
            tlv = client.recv_tlv()
        file_object.close()
Пример #5
0
def thread_handler(client, addr):
    while True:
        tlv = client.recv_tlv()
        if tlv.tag == tls.REQ_CLS:
            client.close()
            print('client {0} is closed'.format(addr[0]))
            break
        elif tlv.tag == tls.REQ_W_DIR:
            print('client {0} reqesting working directory'.format(addr[0]))
            client.send_tlv(tls.TLV(tls.RES_CMD_OK, str(client.working_dir)))
        elif tlv.tag == tls.REQ_DIR:
            files = os.listdir(client.working_dir)
            print('client {0} accessing {1}'.format(addr[0],
                                                    client.working_dir))
            client.send_tlv(tls.TLV(tls.RES_DIR, str(files)))
        elif tlv.tag == tls.REQ_W_DIR_CHANGE:
            nw_dir = client.working_dir
            if os.path.isabs(tlv.val):
                nw_dir = tlv.val
            else:
                nw_dir = os.path.join(client.working_dir, tlv.val)
            try:
                os.listdir(nw_dir)
                client.working_dir = nw_dir
                client.send_tlv(tls.TLV(tls.RES_CMD_OK))
                print('client {0} change working dir to {1}'.format(
                    addr[0], client.working_dir))
            except FileNotFoundError:
                client.send_tlv(
                    tls.TLV(tls.RES_ERROR, 'request directory error'))
                print('client {0} request {1} bad'.format(addr[0], tlv.val))
                continue
            except OSError:
                client.send_tlv(
                    tls.TLV(tls.RES_ERROR, 'file or directory format error'))
                print('client {0} request {1} bad'.format(addr[0], tlv.val))
                continue
        elif tlv.tag == tls.REQ_FILE:
            file_abs_name = os.path.join(client.working_dir, tlv.val)
            file_name = tlv.val
            try:
                file_size = str(os.stat(file_abs_name).st_size)
                client.send_tlv(tls.TLV(tls.RES_FILE_BEG, file_name))
                client.send_tlv(tls.TLV(tls.RES_FILE_SIZE, file_size))
                file_object = open(file_abs_name, 'rb')
                print('client {0} accessing {1}'.format(
                    addr[0], file_abs_name))
                buff = file_object.read(1024)
                while (buff):
                    client.send_tlv(tls.TLV(tls.RES_FILE_BUF, buff))
                    buff = file_object.read(1024)
                client.send_tlv(tls.TLV(tls.RES_FILE_END))
                print('client {0} sending {1} completed'.format(
                    addr[0], file_abs_name))
            except FileNotFoundError:
                client.send_tlv(tls.TLV(tls.RES_ERROR, 'request file error'))
                print('client {0} request {1} bad'.format(
                    addr[0], file_abs_name))
                continue

        elif tlv.tag == tls.REQ_UPL_FILE:
            client.sendtlv(tls.TLV(tls.RES_UPL_FILE_OK))
            tlv = client.recvtlv()
            if tlv.tag == tls.RES_FILE_BEG:
                file_abs_name = tlv.val
                file_name = os.path.basename(file_abs_name)
                file_object = open(file_name, 'wb')
                print('client {0} uploading {1}'.format(addr[0], file_name))
                tlv = client.recvtlv()
                if tlv.tag == tls.RES_FILE_SIZE:
                    file_size = eval(tlv.val)
                tlv = client.recvtlv()
                while tlv.tag != tls.RES_FILE_END:
                    file_object.write(tlv.val)
                    tlv = client.recvtlv()
            print('client {0} uploading {1} completed'.format(
                addr[0], file_name))
            file_object.close()
Пример #6
0
def command_cls(client, args):
    client.send_tlv(tls.TLV(tls.REQ_CLS))
    sys.exit()