-
-
Save hasherezade/6a19f89e3da64815d8e5e231c187be73 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/python3 | |
"""Simple HTTP Server. | |
This module builds on BaseHTTPServer by implementing the standard GET | |
and HEAD requests in a fairly straightforward manner. | |
""" | |
__version__ = "0.7" | |
__all__ = ["SimpleHTTPRequestHandler"] | |
import os | |
import posixpath | |
import http.server | |
import urllib.request, urllib.parse, urllib.error | |
import cgi | |
import html | |
import sys | |
import mimetypes | |
import zlib | |
from optparse import OptionParser | |
try: | |
from io import StringIO | |
except ImportError: | |
from io import StringIO | |
SERVER_PORT = 80 | |
encoding_type = 'text' | |
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='text') | |
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', 'text']: | |
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(http.server.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) | |
print(encoding_type) | |
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) | |
else: | |
print("No encoding") | |
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() | |
self.wfile.write(content) | |
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 = html.escape(urllib.parse.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.parse.quote(linkname), html.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.parse.unquote(path)) | |
words = path.split('/') | |
words = [_f for _f in words if _f] | |
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 = http.server.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 = http.server.HTTPServer(server_address, SimpleHTTPRequestHandler) | |
sa = httpd.socket.getsockname() | |
print("Serving HTTP on", sa[0], "port", sa[1], "...") | |
httpd.serve_forever() | |
http.server.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