title: Python sending a file over sockets
body: <p>I am trying to write a script that requires files to be sent from server to clients and am having issues with the files being completely sent when the destination is not the local host. So when the server and client are on different machines the client seems to receive the file faster then the server is sending it and stops receiving early. An example is if I try to send a 200 MB file over the client stops receiving it around usually around 180 MB.</p>
  
  <p>Here are the portions of the script that send and receive files.</p>
  
  <p>send file</p>
  
  <pre><code>def t_handle(client, address, s):
  file_size = str(os.stat("path to file to send").st_size)
  print file_size
  client.send(file_size)
  work_file = open("path to fil to send", "rb")
  print "sending file"
  file_size = int(file_size)
  while file_size &gt; 0:
      buffer = work_file.read(1024)
      client.send(buffer)
      file_size -= 1024
      print file_size
  print "done"
  </code></pre>
  
  <p>Receive file</p>
  
  <pre><code>def main():
  server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  server.settimeout(90)
  server.connect((host, port))
  file_size = int(server.recv(1024))
  print "receiving file"
  file_size = int(file_size)
  print file_size
  f = open("test_output.mkv", "wb+")
  while file_size &gt; 0:
      work_file = server.recv(1024)
      f.write(work_file)
      file_size -= 1024
      print file_size
  f.close()
  print "done"
  return 0
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>