Let us create a web server that serves contents after compressing it to the gzip format.
Listing 4.8 explains the HTTP compression as follows:
#!/usr/bin/env python # Python Network Programming Cookbook -- Chapter - 4 # This program requires Python 3.5.2 or any later version # It may run on any other version with/without modifications. # # Follow the comments inline to make it run on Python 2.7.x. import argparse import string import os import sys import gzip import io # Comment out the above line and uncomment the below for Python 2.7.x. #import cStringIO from http.server import BaseHTTPRequestHandler, HTTPServer # Comment out the above line and uncomment the below for Python 2.7.x. #from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer DEFAULT_HOST = '127.0.0.1' DEFAULT_PORT = 8800 HTML_CONTENT = b"""<html><body><h1>Compressed Hello World!</h1></body></html>""" # Comment out the above line and uncomment the below for Python 2.7.x. #HTML_CONTENT = b"""<html><body><h1>Compressed Hello World!</h1></body></html>""" class RequestHandler(BaseHTTPRequestHandler): """ Custom request handler""" def do_GET(self): """ Handler for the GET requests """ self.send_response(200) self.send_header('Content-type','text/html') self.send_header('Content-Encoding','gzip') zbuf = self.compress_buffer(HTML_CONTENT) sys.stdout.write("Content-Encoding: gzip ") self.send_header('Content-Length',len(zbuf)) self.end_headers() # Send the message to browser zbuf = self.compress_buffer(HTML_CONTENT) sys.stdout.write("Content-Encoding: gzip ") sys.stdout.write("Content-Length: %d " % (len(zbuf))) sys.stdout.write(" ") self.wfile.write(zbuf) return def compress_buffer(self, buf): zbuf = io.BytesIO() # Comment out the above line and uncomment the below for
Python 2.7.x. #zbuf = cStringIO.StringIO() zfile = gzip.GzipFile(mode = 'wb',
fileobj = zbuf, compresslevel = 6) zfile.write(buf) zfile.close() return zbuf.getvalue() if __name__ == '__main__': parser = argparse.ArgumentParser(description='Simple HTTP
Server Example') parser.add_argument('--port', action="store",
dest="port", type=int, default=DEFAULT_PORT) given_args = parser.parse_args() port = given_args.port server_address = (DEFAULT_HOST, port) server = HTTPServer(server_address, RequestHandler) server.serve_forever()
You can execute this script and see the Compressed Hello World! text (as a result of the HTTP compression) on your browser screen when accessing http://localhost:8800 as follows:
$ python 4_8_http_compression.py localhost - - [22/Feb/2014 12:01:26] "GET / HTTP/1.1" 200 - Content-Encoding: gzip Content-Encoding: gzip Content-Length: 71 localhost - - [22/Feb/2014 12:01:26] "GET /favicon.ico HTTP/1.1" 200 - Content-Encoding: gzip Content-Encoding: gzip Content-Length: 71
The following screenshot illustrates serving compressed content by a web server:
Compressed Content in a Web Server