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?