Sending files between two systems using sockets with python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Sending files between two systems using sockets with python

I need a way to be able to connect two laptops on a network using the python socket module, i can already make a server and a client but how do i send files across them

2nd Jun 2019, 8:22 AM
Owen Jay
Owen Jay - avatar
5 Answers
+ 3
You need to open the file, then take the length of the file and send it to the receiver. Open the file in binary mode (rb). Then, you read the file by block (using seek and read function) and you send the whole file block by block. The receiver knows the file's length, so it knows when it's the file end.
2nd Jun 2019, 10:52 AM
Théophile
Théophile - avatar
+ 3
Thank you very much This was helpful But what about a case in which the files are images
2nd Jun 2019, 10:31 PM
Owen Jay
Owen Jay - avatar
+ 2
Hi Owen, you can read/write image files as easily as you can read/write text files in python. Do the same thing with image files as you did with text files.
3rd Jun 2019, 7:01 AM
777
777 - avatar
+ 1
That isn't a problem because you open the file in binary mode, and all datas sent must be in binary.
3rd Jun 2019, 6:09 AM
Théophile
Théophile - avatar
0
1 - make an http.server by "python -m http.server" 2 - upload the images there 3 - get your private ip 4 - open that ip on the other computer 5 - download the images
3rd Jun 2019, 4:08 PM
Roj Serbest