Server-side code. Opens a file, reads a bit of the file, sends that bit, till it is finished
f = open("{}".format(filename), "rb")
l = f.read(1024)
while (l):
print("Sending...")
client.send(l)
l = f.read(1024)
print("Finished sending")
f.close()
client.shutdown(socket.SHUT_WR)
client.close()
Client-side code, creates the file receives a bit of it, writes it to the file until it is finished.
f = open("{}".format(filename), "ab+")
l = client.recv(1024)
while (l):
print("Receiving...")
f.write(l)
l = client.recv(1024)
f.close()
print("Transmission completed")
- I know that I have to use
socket.shutdown(socket.SHUT_WR)to notify the reciever that the file has finished, but further sends are disallowed. Is there a way to bypass that? Maybe use another command like.shutdown()? I need to keep the connection for further sends/receives. - When dealing with complex files, somehow I receive more than I should. I am sure than the client writes irrelevant previous data to the file, received with
client.recv(1024). Is there any way to "throw away", or empty the data safely before the file's data arrive?
.jpg file: https://i.stack.imgur.com/kjXBZ.png
.txt file: https://i.stack.imgur.com/Yb8j5.png