0

I have a file that I make and zip up using the gzip module, the next thing I want to do is send that file into a socket that connects to a web browser. I know I have to change the Http response content-type to application/gzip, but how do I go about actually sending the binary of the gzip so the browser will download it properly?

Here is what I got so far

f_out = gzip.open('downloads.gz', 'wb')
f_in = open('file.txt', 'rb')
f_out.writelines(f_in)
f_in.close()
f_out.close()
#This all works
.....
def make_http_response(self, content, html_type='text/html'):
        response_headers = {
            'Content-Type': html_type+'; encoding=utf8',
            'Content-Length': len(content),
            'Connection': 'close',
        }
        response_headers_string = ''.join('%s: %s\n' % (k, v) for k, v in   response_headers.iteritems())
        response_proto = 'HTTP/1.1'
        if(self.error):
            response_status = '404'
            response_status_text = 'Not Found' 
        else:
            response_status = '200'
            response_status_text = 'OK '
        response = '%s %s %s' %(response_proto, response_status, response_status_text)
        response = response + response_headers_string + '\n' + str(content)
        return response   

I pass in 'application/gz' for the html_type field

The problem is what content do I pass in I have a download.gz file in that I just created in the same directory how do I go about sending the binary to the make http response function? Thanks

4

1 回答 1

1

只需将文件的二进制原始数据放在消息正文中,无需任何修改。

于 2012-09-12T08:39:39.307 回答