1

I am working on a client-server file sharing to update a particular folder. Where the client connects to server using tcp-socket and recieve a particular zip-file then unzip it. I have accomplished doing that so far but I want to compare the files in both folders(client and server) to check for difference in files contents(ie: updated files) and only download the files if the contents are different.

My code:

Client:

import socket
import zipfile
import os

def main():
    host = '192.168.1.8'
    port = 5000
    s = socket.socket()
    s.connect((host, port))
    with open('C:\\file.zip', 'wb') as f:
        while True:
            data = s.recv(1024)
            if not data:
                break
            f.write(data)
    zip_ref = zipfile.ZipFile('C:\\file.zip', 'r')
    zip_ref.extractall('C:\\')
    zip_ref.close()
    os.remove('C:\\file.zip')
    s.close()

if __name__ == '__main__':
    main()

Server:

import socket
from threading import Thread

def send_file(conn, filename):
    with open(filename, 'rb') as f:
        print 'Sending file'
        data = f.read(1024)
        while data:
            conn.send(data)
            data = f.read(1024)
    print 'Finished sending'
    conn.close()


def main():
    host = '192.168.1.8'
    port = 5000
    s = socket.socket()
    s.bind((host, port))
    s.listen(5)
    while True:
        c, addr = s.accept()
        t = Thread(target=send_file, args=(c, 'C:\\file.zip'))
        t.start()

if __name__ == '__main__':
    main()

What I have tried so far:

I tried filecmp.dircmp but It only checks for different files and not different content of files and also I couldn't compare folder from client with folder from server. I also tried to loop through files and use filecmp on each file but I also couldn't compare it with the same file from server.

Is there an efficient way to do this?

1 Answer 1

2

Not sure I understand you are using filecmp to compare contents from the client before downloading from the server. In these cases, usually there are two approaches: Incorporate a protocol to check the modified date of the files in the server (e.g. os.path.getmtime(file_name)) and then be sure to set the modified date when your client downloads the files; or; have the client request hashes for the files and download when the hashes don't match.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! After some testing I finally got it working by using os.path.getmtime(filename) as you suggested. My only concern is that I am currently sending the modified-date from server to client and receiving it as socket.recv(1024), will that be fine for all scenarios? to receive(1024)?
getmtime returns a float, as per this other answer, a float in python is 64 bits: stackoverflow.com/questions/8216088/… but this is a timestamp, therefore the reasonable values you will get will be much smaller than that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.