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?

Mohd
  • 5,523
  • 7
  • 19
  • 30

1 Answers1

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.

Josep Valls
  • 5,483
  • 2
  • 33
  • 67
  • 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)? – Mohd Jun 20 '17 at 16:38
  • `getmtime` returns a float, as per this other answer, a float in python is 64 bits: https://stackoverflow.com/questions/8216088/how-to-check-the-size-of-a-float-in-python but this is a timestamp, therefore the reasonable values you will get will be much smaller than that. – Josep Valls Jun 20 '17 at 23:59