Created
November 8, 2019 18:25
-
-
Save bkeating/8b33afe073ef1372ba1b817932763948 to your computer and use it in GitHub Desktop.
Python's SimpleHttpServer, but w/Gzip support. 🤙
This file contains hidden or 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
#!/usr/bin/python | |
"""Simple HTTP Server. | |
This module builds on BaseHTTPServer by implementing the standard GET | |
and HEAD requests in a fairly straightforward manner. | |
""" | |
__version__ = "0.6" | |
__all__ = ["SimpleHTTPRequestHandler"] | |
import os | |
import posixpath | |
import BaseHTTPServer | |
import urllib | |
import cgi | |
import sys | |
import mimetypes | |
import zlib | |
from optparse import OptionParser | |
try: | |
from cStringIO import StringIO | |
except ImportError: | |
from StringIO import StringIO | |
SERVER_PORT = 8000 | |
encoding_type = 'gzip' | |
def parse_options(): | |
# Option parsing logic. | |
parser = OptionParser() | |
parser.add_option("-e", "--encoding", dest="encoding_type", | |
help="Encoding type for server to utilize", | |
metavar="ENCODING", default='gzip') | |
global SERVER_PORT | |
parser.add_option("-p", "--port", dest="port", default=SERVER_PORT, | |
help="The port to serve the files on", | |
metavar="ENCODING") | |
(options, args) = parser.parse_args() | |
global encoding_type | |
encoding_type = options.encoding_type | |
SERVER_PORT = int(options.port) | |
if encoding_type not in ['zlib', 'deflate', 'gzip']: | |
sys.stderr.write("Please provide a valid encoding_type for the server to utilize.\n") | |
sys.stderr.write("Possible values are 'zlib', 'gzip', and 'deflate'\n") | |
sys.stderr.write("Usage: python GzipSimpleHTTPServer.py --encoding=<encoding_type>\n") | |
sys.exit() | |
def zlib_encode(content): | |
zlib_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS) | |
data = zlib_compress.compress(content) + zlib_compress.flush() | |
return data | |
def deflate_encode(content): | |
deflate_compress = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS) | |
data = deflate_compress.compress(content) + deflate_compress.flush() | |
return data | |
def gzip_encode(content): | |
gzip_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16) | |
data = gzip_compress.compress(content) + gzip_compress.flush() | |
return data | |
class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
"""Simple HTTP request handler with GET and HEAD commands. | |
This serves files from the current directory and any of its | |
subdirectories. The MIME type for files is determined by | |
calling the .guess_type() method. | |
The GET and HEAD requests are identical except that the HEAD | |
request omits the actual contents of the file. | |
""" | |
server_version = "SimpleHTTP/" + __version__ | |
def do_GET(self): | |
"""Serve a GET request.""" | |
content = self.send_head() | |
if content: | |
self.wfile.write(content) | |
def do_HEAD(self): | |
"""Serve a HEAD request.""" | |
content = self.send_head() | |
def send_head(self): | |
"""Common code for GET and HEAD commands. | |
This sends the response code and MIME headers. | |
Return value is either a file object (which has to be copied | |
to the outputfile by the caller unless the command was HEAD, | |
and must be closed by the caller under all circumstances), or | |
None, in which case the caller has nothing further to do. | |
""" | |
path = self.translate_path(self.path) | |
print("Serving path '%s'" % path) | |
f = None | |
if os.path.isdir(path): | |
if not self.path.endswith('/'): | |
# redirect browser - doing basically what apache does | |
self.send_response(301) | |
self.send_header("Location", self.path + "/") | |
self.end_headers() | |
return None | |
for index in "index.html", "index.htm": | |
index = os.path.join(path, index) | |
if os.path.exists(index): | |
path = index | |
break | |
else: | |
return self.list_directory(path).read() | |
ctype = self.guess_type(path) | |
try: | |
# Always read in binary mode. Opening files in text mode may cause | |
# newline translations, making the actual size of the content | |
# transmitted *less* than the content-length! | |
f = open(path, 'rb') | |
except IOError: | |
self.send_error(404, "File not found") | |
return None | |
self.send_response(200) | |
self.send_header("Content-type", ctype) | |
self.send_header("Content-Encoding", encoding_type) | |
fs = os.fstat(f.fileno()) | |
raw_content_length = fs[6] | |
content = f.read() | |
# Encode content based on runtime arg | |
if encoding_type == "gzip": | |
content = gzip_encode(content) | |
elif encoding_type == "deflate": | |
content = deflate_encode(content) | |
elif encoding_type == "zlib": | |
content = zlib_encode(content) | |
compressed_content_length = len(content) | |
f.close() | |
self.send_header("Content-Length", max(raw_content_length, compressed_content_length)) | |
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime)) | |
self.end_headers() | |
return content | |
def list_directory(self, path): | |
"""Helper to produce a directory listing (absent index.html). | |
Return value is either a file object, or None (indicating an | |
error). In either case, the headers are sent, making the | |
interface the same as for send_head(). | |
""" | |
try: | |
list = os.listdir(path) | |
except os.error: | |
self.send_error(404, "No permission to list directory") | |
return None | |
list.sort(key=lambda a: a.lower()) | |
f = StringIO() | |
displaypath = cgi.escape(urllib.unquote(self.path)) | |
f.write('<!DOCTYPE html>') | |
f.write("<html>\n<title>Directory listing for %s</title>\n" % displaypath) | |
f.write("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath) | |
f.write("<hr>\n<ul>\n") | |
for name in list: | |
fullname = os.path.join(path, name) | |
displayname = linkname = name | |
# Append / for directories or @ for symbolic links | |
if os.path.isdir(fullname): | |
displayname = name + "/" | |
linkname = name + "/" | |
if os.path.islink(fullname): | |
displayname = name + "@" | |
# Note: a link to a directory displays with @ and links with / | |
f.write('<li><a href="%s">%s</a>\n' | |
% (urllib.quote(linkname), cgi.escape(displayname))) | |
f.write("</ul>\n<hr>\n</body>\n</html>\n") | |
length = f.tell() | |
f.seek(0) | |
self.send_response(200) | |
encoding = sys.getfilesystemencoding() | |
self.send_header("Content-type", "text/html; charset=%s" % encoding) | |
self.send_header("Content-Length", str(length)) | |
self.end_headers() | |
return f | |
def translate_path(self, path): | |
"""Translate a /-separated PATH to the local filename syntax. | |
Components that mean special things to the local file system | |
(e.g. drive or directory names) are ignored. (XXX They should | |
probably be diagnosed.) | |
""" | |
# abandon query parameters | |
path = path.split('?',1)[0] | |
path = path.split('#',1)[0] | |
path = posixpath.normpath(urllib.unquote(path)) | |
words = path.split('/') | |
words = filter(None, words) | |
path = os.getcwd() | |
for word in words: | |
drive, word = os.path.splitdrive(word) | |
head, word = os.path.split(word) | |
if word in (os.curdir, os.pardir): continue | |
path = os.path.join(path, word) | |
return path | |
def guess_type(self, path): | |
"""Guess the type of a file. | |
Argument is a PATH (a filename). | |
Return value is a string of the form type/subtype, | |
usable for a MIME Content-type header. | |
The default implementation looks the file's extension | |
up in the table self.extensions_map, using application/octet-stream | |
as a default; however it would be permissible (if | |
slow) to look inside the data to make a better guess. | |
""" | |
base, ext = posixpath.splitext(path) | |
if ext in self.extensions_map: | |
return self.extensions_map[ext] | |
ext = ext.lower() | |
if ext in self.extensions_map: | |
return self.extensions_map[ext] | |
else: | |
return self.extensions_map[''] | |
if not mimetypes.inited: | |
mimetypes.init() # try to read system mime.types | |
extensions_map = mimetypes.types_map.copy() | |
extensions_map.update({ | |
'': 'application/octet-stream', # Default | |
'.py': 'text/plain', | |
'.c': 'text/plain', | |
'.h': 'text/plain', | |
}) | |
def test(HandlerClass = SimpleHTTPRequestHandler, | |
ServerClass = BaseHTTPServer.HTTPServer): | |
"""Run the HTTP request handler class. | |
This runs an HTTP server on port 8000 (or the first command line | |
argument). | |
""" | |
parse_options() | |
server_address = ('0.0.0.0', SERVER_PORT) | |
SimpleHTTPRequestHandler.protocol_version = "HTTP/1.0" | |
httpd = BaseHTTPServer.HTTPServer(server_address, SimpleHTTPRequestHandler) | |
sa = httpd.socket.getsockname() | |
print "Serving HTTP on", sa[0], "port", sa[1], "..." | |
httpd.serve_forever() | |
BaseHTTPServer.test(HandlerClass, ServerClass) | |
if __name__ == '__main__': | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The max() function used for content-length is unnecessary and causes an invalid content-length condition that results in some clients aborting the connection as a failed transfer because the content-length value in the header doesn't match the data transferred.
self.send_header("Content-Length", max(raw_content_length, compressed_content_length))
Compressed data will either be smaller or potentially larger than the original length, but that is OK. There are no logic checks to determine if compression was actually performed, so direct use of the "compressed" content length works in all scenarios.
self.send_header("Content-Length", compressed_content_length)