예제 #1
0
def main():
    host = '127.0.0.1'
    port = 8083
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind((host, port))
    print("server ip " + str(host))
    print("bind socket port: %s" % (port))

    listData = list_local(root)

    for file in listData:
        filename = os.path.join(root, file)
        hashData = generate_file_md5(filename)
        list_hash.append(hashData)

    try:
        thread_listen_client = threading.Thread(target=listen_client, kwargs={
                                                "clientsocket": server_socket})
        thread_listen_client.start()
    except:
        print("Error in thread: listen_client")
예제 #2
0
def main():
    global newListHash, list_hash
    host = '127.0.0.1'
    port = 8083
    listData = list_local(root)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))
    for file in listData:
        filename = os.path.join(root, file)
        hashData = generate_file_md5(filename)
        list_hash.append(hashData)

    while True:

        listDataNew = list_local(root)

        for file in listDataNew:
            filename = os.path.join(root, file)
            hashData = generate_file_md5(filename)
            newListHash.append(hashData)

        change = updateList(list_hash, newListHash)
        list_hash = list_hash + change
        panjang = len(change)
        if (panjang != 0):
            for file in listDataNew:
                for hash_in in change:
                    filename = os.path.join(root, file)
                    hashData = generate_file_md5(filename)
                    if (hash_in == hashData):
                        s.send(('update').encode('unicode_escape'))
                        reply = s.recv(1024).decode('unicode_escape')
                        if ('ack' in reply):
                            print('ack_update')
                            upload(s, filename, file, hash_in)
                            print('success')
                        else:
                            pass
        else:
            s.send(('isUpdate').encode('latin-1'))
            reply = ''
            jum = 0
            count = 0
            jum = s.recv(1024).decode('latin-1')
            if ('kosong' in jum):
                listDataNew = list_local(root)
                for file in listDataNew:
                    filename = os.path.join(root, file)
                    hashData = generate_file_md5(filename)
                    s.send(('update').encode('unicode_escape'))
                    reply = s.recv(1024).decode('unicode_escape')
                    if ('ack' in reply):
                        upload(s, filename, file, hashData)
                        print('success')
                    else:
                        pass

            elif ('noUpdate' in jum):
                print(jum)
            elif (any(i.isdigit() for i in jum) and len(jum) < 5):
                jum = int(''.join(filter(str.isdigit, jum)))
            else:
                pass

            if (jum != 0):
                while True:
                    reply = ''
                    reply = s.recv(1024).decode('latin-1')
                    if ('update' in reply):
                        count += 1
                        if (reply[1] in list_hash):
                            s.sendall(('file exist').encode('utf-8'))
                        else:
                            s.sendall(('ack').encode('utf-8'))
                            download(s, root, list_hash)
                            print('update')
                    elif ('noUpdate' in reply):
                        print('ok')
                    if (count == jum):
                        break
            else:
                pass
        time.sleep(10)
예제 #3
0
def recieve_from_client(socket):
    global list_hash, listDataNew, newList_hash
    while True:
        command = ''
        command = socket.recv(1024).decode('latin-1')

        listDataNew = list_local(root)
        for file in listDataNew:
            filename = os.path.join(root, file)
            hashData = generate_file_md5(filename)
            newList_hash.append(hashData)

        print('REQ diterima '+str(command))

        if ('update' in command):
            socket.send(('ack').encode('latin-1'))
            download(socket, root, newList_hash)
            print('update sucess')
            for file in listDataNew:
                    filename = os.path.join(root, file)
                    hashData = generate_file_md5(filename)
                    if(hashData in newList_hash):
                        pesan = ''
                        pesan = 'update'
                        socket.sendall(pesan.encode())
                        terima = socket.recv(1024).decode('latin-1')
                        if ('ack' in terima):
                            upload(socket, filename, file, hashData)
                            print('success')
                        else:
                            pass
                    else:
                        pass

        elif ('isUpdate' in command):
            listDataNew = list_local(root)
            panjang = len(listDataNew)
            panjang = str(panjang)
            panj = len(panjang)
            if(newList_hash == '' ):
                print('noUpdate')
                socket.send('noUpdate'.encode('utf-8'))
            elif (panjang== '0'):
                print('dir kosong')
                socket.send('kosong'.encode('utf-8'))
            else:
                socket.send(panjang[:5].encode('ascii'))
                for file in listDataNew:
                    filename = os.path.join(root, file)
                    hashData = generate_file_md5(filename)
                    if(hashData in newList_hash):
                        pesan = ''
                        pesan = 'update'
                        socket.sendall(pesan.encode())
                        terima = socket.recv(1024).decode('utf-8')
                        print(terima)
                        if ('ack' in terima):
                            upload(socket, filename, file, hashData)
                            print('success')
                        else:
                            pass
                    else:
                        pass

        else:
            pass