python socket file transfer

后端 未结 5 1776
逝去的感伤
逝去的感伤 2020-12-30 09:16

I\'m trying to write transfer files or chunks of data over a socket. I feel as if I\'m reinventing the wheel, but my searches for a simple solution have failed (everything I

5条回答
  •  天命终不由人
    2020-12-30 09:28

    Your code looks good.

    You don't actually need to send across the size of the file. You can use while True, as the check if not data: break will stop the loop.

    while True:
        data = s.recv(1024)
    
        if not data: print " Done "; break
    
        recvd += data
    

    Also, why are you sending 'ok' is the other side doesn't check for it? You are just skipping 2 bytes at each side.

    Don't you need to cater to multiple clients? No need for multi-threading?

提交回复
热议问题