I'm creating an HTTP server in Python without any of the HTTP libraries for learning purposes. Right now it can serve static files fine.
The way I serve the file is through this piece of code:
with open(self.filename, 'rb') as f:
src = f.read()
socket.sendall(src)
However, I want to optimize its performance a bit by sending compressed data instead of uncompressed. I know that my browser (Chrome) accepts compressed data because it tells me in the header
Accept-Encoding: gzip, deflate, sdch
So, I changed my code to this
with open(self.filename, 'rb') as f:
src = zlib.compress(f.read())
socket.sendall(src)
But this just outputs garbage. What am I doing wrong?