Skip to content Skip to sidebar Skip to footer

Send Big File Over Socket

I have a video file and want to send it over socket. Video is send to the client but video is not playable and also video size received is 2 KB. And acutely video size is 43 MB. W

Solution 1:

Check the return value of send and recv. The 9000000 value is a maximum but not guaranteed value to send/recv. Alternatively, use sendall.

For recv, you have to loop until you receive all the data. If you close the socket after the file is sent, recv will return zero when all the data is received.

FYI, your while True: in both files never loops due to the break, so they are unnecessary.

Here's something that should work...

server.py

import socket

soc = socket.socket()
soc.bind(('',8080))
soc.listen(1)

print('waiting for connection...')
with soc:
    con,addr = soc.accept()
    print('server connected to',addr)
    with con:
        filename = input('enter filename to send: ')
        withopen(filename, 'rb') as file:
            sendfile = file.read()
        con.sendall(sendfile)
        print('file sent')

client.py

import socket

soc = socket.socket()
soc.connect(('localhost',8080))
savefilename = input("enter file name to receive: ")
with soc,open(savefilename,'wb') as file:
    whileTrue:
        recvfile = soc.recv(4096)
        ifnot recvfile: break
        file.write(recvfile)
print("File has been received.")

Post a Comment for "Send Big File Over Socket"