1

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")
  1. 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.
  2. 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

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • maybe it's because of option "ab+". "а" is for appending, not for creating new clear file and write to it as "w" – Speaking Code Aug 25 '15 at 12:37
  • Speaking Code is right. You should use `'wb'` (w for write, b for binary mode). – pepr Aug 25 '15 at 13:35
  • It is nice to use `shutdown`. But for a true [graceful shutdown](https://stackoverflow.com/a/27777498/3545273) , the server program should try to read from the socket until the client (receiver part) has closed the connection. – Serge Ballesta Jul 09 '19 at 06:15
  • You don't have to shutdown...Send the size of the file first, then the file. Read exactly the number of bytes indicated by the size. Then you can send another file. [This answer](https://stackoverflow.com/a/55840341/235698) has an example. – Mark Tolonen Jul 09 '19 at 06:49

1 Answers1

0

I can see two intentions behind the question. First, learning the things; second, implementing something that is cool and that works (usefull). Part of the learning process is getting the passive knowledge via reading the documentation -- this is for opening the file, and using the with construct.

However, part of the learning is also learning a bigger picture, and learning how others (more capable) solve the things. This can be done via reading source codes of the existing solution, and trying to understand the code.

I suggest to get the paramiko module (https://github.com/paramiko/paramiko.git), and to study sftp_client.py, sftp_server.py, and the demos/.

True, it is much more complex than the few lines that you tried, but you may learn a lot, and the knowledge of how to use paramiko may be valuable for you in future. Also, you will find more questions to focus learning to. ;)

pepr
  • 20,112
  • 15
  • 76
  • 139