Пример #1
0
def download(file, soc, user_data):
    user_data['command'] = 'download'
    user_data['Argument'] = file[0]
    pl.sendFile(user_data, soc)
    print 'User: '******'user']
    print 'Downloading file: ', user_data['Argument']
    with open(file[0] + '.zip', 'wb') as f:
        file_size = soc.recv(1024)
        file_size = int(file_size)
        rsize = 0
        soc.send("ok")  # Handshake
        while True:
            print 'Downloading...'
            data = soc.recv(1024)
            rsize += len(data)
            f.write(data)
            if rsize >= file_size:
                break
    # Unzip file
    print 'Download done!'
    dirpath = getcwd()
    zip_ref = zipfile.ZipFile(dirpath + '/' + file[0] + '.zip', 'r')
    zip_ref.extractall(dirpath)
    zip_ref.close()
    remove(user_data['Argument'] + '.zip')
Пример #2
0
def exit(file,soc):
    online = 0
    notify = pl.message([None,None,None,None,None,None,'ok',None])
    pl.sendFile(notify,soc)
    print file['user']," exiting...", "\n\n"
    soc.close()
    return online
Пример #3
0
def goToDir(file,soc):
    arg = file['Argument']
    dirpath = getcwd()
    dir = dirpath.split('/')
    if arg == '..':
        if dir[-2] == 'Home':
            print 'Changing to SharedFolder'
            chdir('..')
            chdir('SharedFolder')
            file['path'] = 'Home/SharedFolder'
        else:
            chdir(arg)
            del dir[-1]
            path = ''
            for i in xrange(len(dir)):
                if dir[i] == 'Home':
                    p = dir[i:len(dir)]
            for i in p:
                path += i + '/'
            file['path'] = path
    else:
        chdir(arg)
        file['path'] = file['path'] + '/' + arg
    pl.sendFile(file,soc)
    return file
Пример #4
0
def exit(soc, user_data):
    user_data['command'] = 'exit'
    pl.sendFile(user_data, soc)
    string = pl.receiveFile(soc)
    if string['data'] == 'ok':
        soc.close()
        print "Exiting..."
        sys.exit()
Пример #5
0
def removeFile(file_name, soc, user_data):
    user_data['command'] = 'rm'
    user_data['Argument'] = file_name
    pl.sendFile(user_data, soc)
    message = pl.receiveFile(soc)
    if message['data'] == 0:
        print("Error: %s file not found" % file_name)
    else:
        print("File %s removed successful" % file_name)
Пример #6
0
def checkDir(soc, user_data):
    user_data['command'] = 'checkdir'
    pl.sendFile(user_data, soc)
    file = pl.receiveFile(soc)
    user_data = json.dumps(file)
    user_data = ast.literal_eval(user_data)
    dir = ast.literal_eval(user_data['data'])
    for i in dir:
        print i
Пример #7
0
def upload(file, soc, user_data):
    root, dirs, files = walk('.').next()
    print 'Files in current client directory:'

    for i in files:
        print i
    print 'Folders in current client directory:'

    for i in dirs:
        print i

    file_count = 0
    found = 0

    try:
        makedirs('temp')
    except OSError as e:
        if e.errno == errno.EEXIST:  # and path.isdir(path)
            pass
    shutil.move(file[0], 'temp')

    for i in files:
        if i == file[0]:
            file_count = 1
            found = 1
    for i in dirs:
        if i == file[0]:
            found = 1
    if found:
        dirpath = getcwd()
        user_data['command'] = 'upload'
        user_data['Argument'] = file[0]
        pl.sendFile(user_data, soc)
        shutil.make_archive(dirpath + '/' + file[0], 'zip', 'temp')
        size = path.getsize(file[0] + '.zip')
        soc.send(str(size))
        Handshake = soc.recv(2)
        if Handshake == 'ok':  # if authorized to send then send
            print "Uploading", file[0], " ..."
            with open(file[0] + '.zip', 'rb') as f:
                data = f.read(1024)
                while data:
                    soc.send(data)
                    data = f.read(1024)
            remove(file[0] + '.zip')
            shutil.rmtree('temp', ignore_errors=True)
        else:
            print "Error to send file", file[0], " to Server."
    else:
        print("Error: %s file not found" % file)
Пример #8
0
def removeFile(file,soc):
    deleted = 0
    root, dirs, files = walk('.').next()
    for i in files:
        if i == file['Argument']:
            remove(file['Argument'])
            deleted = 1
    for i in dirs:
        if i == file['Argument']:
            shutil.rmtree(file['Argument'], ignore_errors=True)
            deleted = 1
    if not deleted:
        print("Error: %s file not found" % file['Argument'])
    notify = pl.message([None,None,None,None,None,None,deleted,None])
    pl.sendFile(notify,soc)
Пример #9
0
def checkUserName(user, password, IP, PORT):
    soc = pl.connectionClient()
    # Handshake
    message = {
        'user': user,
        'password': password,
        'IP': '0.0.0.0',
        'Port': PORT,
        'command': None,
        'Argument': None,
        'data': None,
        'path': None
    }
    pl.sendFile(message, soc)
    file = pl.receiveFile(soc)
    print 'ok'
    return int(file['data']), soc, file
Пример #10
0
def download(file,soc):
    root, dirs, files = walk('.').next()
    found = 0
    file_count = 0
    try:
        makedirs('temp')
    except OSError as e:
        if e.errno == errno.EEXIST: # and path.isdir(path)
            pass
    shutil.move(file['Argument'],'temp')

    for i in files:
        if i == file['Argument']:
            file_count = 1
            found = 1

    for i in dirs:
        if i == file['Argument']:
            found = 1

    if found:
        dirpath = getcwd()
        shutil.make_archive(dirpath + '/' + file['Argument'], 'zip', 'temp')
        size = path.getsize(file['Argument'] + '.zip')
        soc.send(str(size))
        Handshake = soc.recv(2)
        if Handshake == 'ok': # if authorized to send then send
            with open(file['Argument'] + '.zip','rb') as f:
                data = f.read(1024)
                while data:
                    soc.send(data)
                    data = f.read(1024)
            print 'Download done!'
            remove(file['Argument']+'.zip')
            shutil.rmtree('temp', ignore_errors=True)
        else:
            print "Error to send file",file['Argument']," to Server."
    else:
        print("Error: %s file not found" % file)
        file['data'] = 'File not found'
        pl.sendFile(file,soc)
Пример #11
0
def mkDir(args, soc, user_data):
    user_data['command'] = 'makedir'
    user_data['Argument'] = args
    pl.sendFile(user_data, soc)
Пример #12
0
def goToDir(args, soc, user_data):
    user_data['command'] = 'cd'
    user_data['Argument'] = args
    pl.sendFile(user_data, soc)
    user_data = pl.receiveFile(soc)
    return user_data
Пример #13
0
def moveFile(args, soc, user_data):
    user_data['command'] = 'mv'
    user_data['Argument'] = args
    pl.sendFile(user_data, soc)
Пример #14
0
def checkDir(file,soc):
    dir = listdir('.')
    for i in dir:
        print i
    file['data'] = dir
    pl.sendFile(file,soc)