I'm recently working on a project in which I'm basically making a dropbox clone. The server and client are working fine but I'm having a slight issue. I'm able to transfer a single file from the client to the server but when I try to transfer all the files together it gives me an error after the transfer of the first file so basically my code is only working for a single file. I need to make it work for multiple files. Any help will be appreciated. Here's my code
Server Code
import socket
import thread
import hashlibserversock = socket.socket()
host = socket.gethostname();
port = 9000;
serversock.bind((host,port));
filename = ""
serversock.listen(10);
print "Waiting for a connection....."clientsocket,addr = serversock.accept()
print("Got a connection from %s" % str(addr))
while True:size = clientsocket.recv(1)filesz = clientsocket.recv(1)if filesz.isdigit():size += fileszfilesize = int(size)else:filesize = int(size) print filesizefor i in range(0,filesize):if filesz.isdigit():filename += clientsocket.recv(1)else:filename += fileszfilesz = "0"print filename file_to_write = open(filename, 'wb')while True:data = clientsocket.recv(1024)#print dataif not data:breakfile_to_write.write(data)file_to_write.close()print 'File received successfully'
serversock.close()
Client Code
import socket
import os
import threads = socket.socket()
host = socket.gethostname()
port = 9000
s.connect((host, port))
path = "C:\Users\Fahad\Desktop"
directory = os.listdir(path)
for files in directory:print files filename = filessize = bytes(len(filename))#print sizes.send(size)s.send(filename)file_to_send = open(filename, 'rb')l = file_to_send.read()s.sendall(l)file_to_send.close() print 'File Sent'
s.close()
Here's the error that I get
Waiting for a connection.....
Got a connection from ('192.168.0.100', 58339)
13
Betternet.lnk
File received successfully
Traceback (most recent call last):File "server.py", line 22, in <module>filesize = int(size)
ValueError: invalid literal for int() with base 10: ''