Need to transfer multiple files from client to server

2024/9/24 6:31:30

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: ''
Answer

There are several minor issues in your snippet. Maybe you could do something like this?

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(16) # Note that you limit your filename length to 255 bytes.if not size:breaksize = int(size, 2)filename = clientsocket.recv(size)filesize = clientsocket.recv(32)filesize = int(filesize, 2)file_to_write = open(filename, 'wb')chunksize = 4096while filesize > 0:if filesize < chunksize:chunksize = filesizedata = clientsocket.recv(chunksize)file_to_write.write(data)filesize -= len(data)file_to_write.close()print 'File received successfully'
serversock.close()

Client:

import socket
import os
import threads = socket.socket()
host = socket.gethostname()
port = 9000
s.connect((host, port))
path = "blah"
directory = os.listdir(path)
for files in directory:print filesfilename = filessize = len(filename)size = bin(size)[2:].zfill(16) # encode filename size as 16 bit binarys.send(size)s.send(filename)filename = os.path.join(path,filename)filesize = os.path.getsize(filename)filesize = bin(filesize)[2:].zfill(32) # encode filesize as 32 bit binarys.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 the client also sends the size of the file. Both size and filesize are encoded as binary strings (you could do another method). The filename length (size) can take values up to 2^16 and the send file can have up to 2^32 byte (that is 2^filesize).

https://en.xdnf.cn/q/71732.html

Related Q&A

pyplot bar charts with individual data points

I have data from a control and treatment group. Is matplotlib able to create a bar chart where the bar height is the mean of each group overlaid with the individual data points from that group? Id lik…

python only works with sudo

My python 2.7 script works on my Ubuntu system if I call it using sudo python [filename].pyor from a bash script using sudo ./[bashscriptname].shBut if I call it from Pycharm I get oauth errors, and fr…

Forbidden (CSRF token missing or incorrect) Django error

I am very new to Django. The name of my project is rango and I have created a URL named /rango/tagger that is supposed to send an object. In my java-script, I have tried to communicate with this route …

How to update artists in scrollable, matplotlib and multiplot

Im trying to create a scrollable multiplot based on the answer to this question: Creating a scrollable multiplot with pythons pylabLines created using ax.plot() are updating correctly, however Im unabl…

Sending Keys Using Splinter

I want to test an autocomplete box using Splinter. I need to send the down and enter keys through to the browser but Im having trouble doing this. I am currently finding an input box and typing tes int…

Split lists within dataframe column into multiple columns [duplicate]

This question already has answers here:Split a Pandas column of lists into multiple columns(13 answers)Closed 3 years ago.I have a Pandas DataFrame column with multiple lists within a list. Something l…

Drawing with turtle(python) using PyCharm

Im running the latest PyCharm Pro version and trying to run the below code from a scratch file but it doesnt seem to work import turtlewn = turtle.Screen() alex = turtle.Turtle() alex.forward(150) a…

How to adapt my current splash screen to allow other pieces of my code to run in the background?

Currently I have a splash screen in place. However, it does not work as a real splash screen - as it halts the execution of the rest of the code (instead of allowing them to run in the background).This…

Reversed array slice including the first element [duplicate]

This question already has answers here:Python reverse-stride slicing(8 answers)Closed 5 years ago.Lets say I have:>>> a = [1, 2, 3, 4]And I want to get a reversed slice. Lets say I want the 1s…

Problem using py2app with the lxml package

I am trying to use py2app to generate a standalone application from some Python scripts. The Python uses the lxml package, and Ive found that I have to specify this explicitly in the setup.py file that…