python - Need to transfer multiple files from client to server -


i'm working on project in i'm making dropbox clone. server , client working fine i'm having slight issue. i'm able transfer single file client server when try transfer files gives me error after transfer of first file code working single file. need make work multiple files. appreciated. here's code

server code

import socket import thread import hashlib  serversock = socket.socket() host = socket.gethostname(); port = 9000; serversock.bind((host,port)); filename = "" serversock.listen(10); print "waiting connection....."  clientsocket,addr = serversock.accept()  print("got connection %s" % str(addr)) while true:     size = clientsocket.recv(1)     filesz = clientsocket.recv(1)     if filesz.isdigit():         size += filesz         filesize = int(size)     else:         filesize = int(size)         print filesize     in range(0,filesize):         if filesz.isdigit():             filename += clientsocket.recv(1)         else:             filename += filesz             filesz = "0"     print filename           file_to_write = open(filename, 'wb')     while true:         data = clientsocket.recv(1024)         #print data         if not data:             break         file_to_write.write(data)     file_to_write.close()     print 'file received successfully' serversock.close() 

client code

import socket import os import thread  s = socket.socket()  host = socket.gethostname()                            port = 9000 s.connect((host, port)) path = "c:\users\fahad\desktop" directory = os.listdir(path) files in directory:     print files       filename = files     size = bytes(len(filename))     #print size     s.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 error get

waiting connection..... got connection ('192.168.0.100', 58339) 13 betternet.lnk file received traceback (most recent call last):   file "server.py", line 22, in <module>     filesize = int(size) valueerror: invalid literal int() base 10: '' 

there several minor issues in snippet. maybe this?

import socket import thread import hashlib  serversock = socket.socket() host = socket.gethostname(); port = 9000; serversock.bind((host,port)); filename = "" serversock.listen(10); print "waiting connection....."  clientsocket,addr = serversock.accept() print("got connection %s" % str(addr)) while true:     size = clientsocket.recv(16) # note limit filename length 255 bytes.     if not size:         break     size = int(size, 2)     filename = clientsocket.recv(size)     filesize = clientsocket.recv(32)     filesize = int(filesize, 2)     file_to_write = open(filename, 'wb')     chunksize = 4096     while filesize > 0:         if filesize < chunksize:             chunksize = filesize         data = clientsocket.recv(chunksize)         file_to_write.write(data)         filesize -= chunksize      file_to_write.close()     print 'file received successfully' serversock.close() 

client: import socket import os import thread

s = socket.socket() host = socket.gethostname() port = 9000 s.connect((host, port)) path = "blah" directory = os.listdir(path) files in directory:     print files     filename = files     size = len(filename)     size = bin(size)[2:].zfill(16) # encode filename size 16 bit binary     s.send(size)     s.send(filename)      filename = os.path.join(path,filename)     filesize = os.path.getsize(filename)     filesize = bin(filesize)[2:].zfill(32) # encode filesize 32 bit binary     s.send(filesize)      file_to_send = open(filename, 'rb')      l = file_to_send.read()     s.sendall(l)     file_to_send.close()     print 'file sent'  s.close() 

here client sends size of file. both size , filesize encoded binary strings (you method). filename length (size) can take values 2^16 , send file can have 2^32 byte (that 2^filesize).


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -