简体   繁体   中英

Get SimpleHTTPServer to send response headers

I'm using this class to run a HTTP-server in Python:

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)

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().

I've seen this answer and edited my code accordingly:

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])

But that doesn't make any difference. The response headers are still stuffed in the file. What's going on here?

Edit: 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.

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).

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.

Ok, using Wireshark I found out what's actually wrong here:

I forgot to do a

self.send_response(200)

before sending the headers, which sends a line like:

HTTP/1.1 200 OK

I guess the absence of that line causes HTTP-clients to believe the server is using HTTP/0.9 and in effect get the headers wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM