title: Get SimpleHTTPServer to send response headers
body: <p>I'm using this class to run a HTTP-server in Python:</p>
  
  <pre><code>class Server:
  
      def __init__(self):
          port = 81
          httpd = SocketServer.ThreadingTCPServer(('', port), self.Proxy)
          httpd.serve_forever()
  
      class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
  
          def do_GET(self):
              # just for demonstration:
              url = "http://ovh.net/files/10Mio.dat"
              opener = urllib2.build_opener()
              handle = opener.open(url)
              if hasattr(handle.info(), "headers"):
                  headers = handle.info().headers
                  for h in headers:
                      self.send_header(h[0], h[1])
              self.end_headers()
              self.copyfile(handle, self.wfile)
  </code></pre>
  
  <p>But unfortunately that doesn't work as expected. If I send a request to the server, I'm getting a file that's a bit bigger than the one I would expect. When I look into it, I see that the response headers are stored at the beginning of that file. The response itself contains no headers, so I think SimpleHTTPServer ended the headers before calling do_GET().</p>
  
  <p>I've seen <a href="https://stackoverflow.com/a/13354482/1024057">this answer</a> and edited my code accordingly:</p>
  
  <pre><code>class Server:
  
      def __init__(self):
          port = 81
          httpd = SocketServer.ThreadingTCPServer(('', port), self.Proxy)
          httpd.serve_forever()
  
  
  
      class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
  
          def do_GET(self):
              # get handle again
              self.copyfile(handle, self.wfile)
  
          def end_headers(self):
              self.send_my_headers()
              SimpleHTTPServer.SimpleHTTPRequestHandler.end_headers(self)
  
          def send_my_headers(self):
              # Do something smart to get a file-like variable named handle
              self.send_header("Access-Control-Allow-Origin", "*")
              if hasattr(handle.info(), "headers"):
                  headers = handle.info().headers
                  for h in headers:
                      self.send_header(h[0], h[1])
  </code></pre>
  
  <p>But that doesn't make any difference. The response headers are still stuffed in the file. What's going on here?</p>
  
  <p><strong>Edit:</strong>
  I updated my first example to clarify what I mean. What I want is a bit like an HTTP-proxy: My server should download a file from another server and just send the data back to the client. For some reasons (would be too much to explain) I can't use a regular proxy.</p>
  
  <p>Now the first code example does that quite efficiently. My problem there is that all the headers get lost in the process. That means the client is unaware of the download size (thus showing an indefinite progress bar) or the file name (Content-disposition).</p>
  
  <p>In my second code example, I tried to send the headers that I got from the upstream server but the headers seem to be finished before do_GET is called.</p>