title: python sending http response
body: <p>I am trying to write a <em>very</em> simple HTTP server which sends a video being streamed server side.  When a client connects, the get_video program (fictional) is started in another process and its stdout is being piped to us (under the assumption get_video sends/streams video to stdout).  I am using <code>subprocess.Popen()</code> for this.</p>
  
  <pre><code>import subprocess, socket
  
  def send_video(sock, programme_id):
      p = subprocess.Popen(["get_video","--pid",programme_id], stdout=subprocess.PIPE)
      sock.send("HTTP/1.1 200 OK\nContent-type: application/octet-stream\n\n")
      while True:
          chunk = p.stdout.read(1024)
          if chunk:
              try:
                  sock.send(chunk)
              except Exception:
                  pass
          else:
              break
  
  def main():
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.bind(('',8080))
      s.listen(5)
  
      while True:
          client, address = s.accept()
          data = client.recv(1024)
          send_video(client, "123456")
          client.close()
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p>This barely works.  If I issue an HTTP request using <code>wget <a href="http://localhost:8080/blah.mp4" rel="nofollow">http://localhost:8080/blah.mp4</a></code> the whole thing does as expected - the video is streamed to the client's socket and appended to the new file, blah.mp4.</p>
  
  <p>However, if I create a dummy HTML page with <code>&lt;a href="http://localhost:8080/blah/mp4"&gt;download&lt;/a&gt;</code> and then try 'save target/link as...' on that link, the get_video program is invoked twice.  The second time being when the video is actually sent to the client's socket.  There must be some sort of buffering going on.</p>
  
  <p>Notice the <code>try/except</code> block in <code>send_video()</code>.  I was getting a 'broken pipe' error before (with the dummy HTML page method), indicating the client's socket wasn't there to be written to.  I put the <code>try/except</code> there to try and ignore it.</p>
  
  <p>I am fairly confused.  The HTTP requests look the same, I'm not sure what my browser (Firefox) is doing differently to cause this.</p>
  
  <p>Any ideas?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>The header from wget:</p>
  
  <pre><code>GET /blah.mp4 HTTP/1.0
  User-Agent: Wget/1.12 (linux-gnu)
  Accept: */*
  Host: 192.168.1.2:8080
  Connection: Keep-Alive
  </code></pre>
  
  <p>The header from html dummy page:</p>
  
  <pre><code>GET /blah.mp4 HTTP/1.1
  Host: 192.168.1.2:8080
  User-Agent: Mozilla/5.0 (blah) Gecko/blah Firefox/3.6.16
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  Accept-Language: en-gb,en;q=0.5
  Accept-Encoding: gzip,deflate
  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  Keep-Alive: 115
  Connection: keep-alive
  Pragma: no-cache
  Cache-Control: no-cache
  </code></pre>
  
  <p>The python server reported only one of each, but Wireshark reported 2 GET requests for the dummy html link method.  I don't see anything obvious here...</p>