Created
August 1, 2018 15:12
-
-
Save subzey/22f4296110afde371490ec3bf8525005 to your computer and use it in GitHub Desktop.
Precompressed header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import BaseHTTPServer | |
import io | |
import random | |
import time | |
import zlib | |
HOST_NAME = 'localhost' | |
PORT_NUMBER = 8080 | |
gzip = zlib.compressobj( | |
zlib.Z_BEST_COMPRESSION, | |
zlib.DEFLATED, | |
0x10 | zlib.MAX_WBITS | |
) | |
header = '' | |
trailer = '' | |
gzipped_header = '' | |
with io.open('template.html', mode='r', encoding='utf-8') as fp: | |
(header, _, trailer) = fp.read().rpartition('{{number}}') | |
gzipped_header += gzip.compress(header) | |
gzipped_header += gzip.flush(zlib.Z_SYNC_FLUSH) | |
def html_gen(): | |
# Dump precompressed data | |
yield gzipped_header | |
# Imitating DB request and stuff | |
time.sleep(3) | |
# Copying the gzip compressobj with state | |
gzip_copy = gzip.copy() | |
# Writing the gzipped payload | |
yield gzip_copy.compress( | |
str( | |
random.randrange(1, 100) | |
) | |
) | |
# Writing the gzipped trailer | |
yield gzip_copy.compress( | |
trailer | |
) | |
# Flushing the last buffered bytes in compressobj | |
yield gzip_copy.flush(zlib.Z_FINISH) | |
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
def do_GET(self): | |
if self.path != '/': | |
self.send_response(404) | |
self.end_headers() | |
return | |
"""Respond to a GET request.""" | |
self.send_response(200) | |
self.send_header("content-type", "text/html") | |
self.send_header("content-encoding", "gzip") | |
self.end_headers() | |
for chunk in html_gen(): | |
self.wfile.write(chunk) | |
if __name__ == '__main__': | |
server_class = BaseHTTPServer.HTTPServer | |
httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler) | |
print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER) | |
try: | |
httpd.serve_forever() | |
except KeyboardInterrupt: | |
pass | |
httpd.server_close() | |
print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Your lucky number</title> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
margin: 1em; | |
font: 16pt sans-serif; | |
background: #eee; | |
} | |
h1 { | |
margin: 1em; | |
font-weight: normal; | |
text-align: center; | |
font-size: 32pt; | |
color: #333; | |
} | |
.number { | |
color: #000; | |
display: block; | |
margin: 1em; | |
font-size: 2em; | |
font-weight: bold; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Your lucky number for today is <span class="number">{{number}}</span></h1> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment