-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from Grovkillen/dash_functionality
Dash functionality #2
- Loading branch information
Showing
11 changed files
with
1,052 additions
and
467 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,313 @@ | ||
"""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 | ||
import webbrowser | ||
from optparse import OptionParser | ||
|
||
try: | ||
from cStringIO import StringIO | ||
except ImportError: | ||
from StringIO import StringIO | ||
|
||
SERVER_PORT = 8000 | ||
encoding_type = 'gzip' | ||
source = '' | ||
source_files = ['index.html', 'index.htm', 'index.html.gz', 'index.htm.gz', 'index.gz'] | ||
|
||
def parse_options(): | ||
# Option parsing logic. | ||
parser = OptionParser() | ||
global encoding_type | ||
parser.add_option("-e", "--encoding", dest="encoding_type", | ||
help="Encoding type for server to utilize", | ||
metavar=encoding_type, default=encoding_type) | ||
global SERVER_PORT | ||
parser.add_option("-p", "--port", dest="port", default=SERVER_PORT, | ||
help="The port to serve the files on", | ||
metavar=SERVER_PORT) | ||
global source | ||
parser.add_option("-s", "--source", dest="source", | ||
help="Pick what file to serve ['index.html', 'index.htm', 'index.html.gz', 'index.htm.gz', 'index.gz'], default will serve in this order", | ||
metavar="index.htm.gz", default='') | ||
(options, args) = parser.parse_args() | ||
encoding_type = options.encoding_type | ||
SERVER_PORT = int(options.port) | ||
source = options.source | ||
global source_type | ||
base, ext = posixpath.splitext(source) | ||
ext = ext.lower() | ||
if ext == '.gz': | ||
source_type = 'gzipped' | ||
else: | ||
source_type = 'normal' | ||
global source_files | ||
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) | ||
f = None | ||
type = 'normal' | ||
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 | ||
if not source == '': | ||
index = os.path.join(path, source) | ||
if os.path.exists(index): | ||
path = index | ||
else: | ||
return self.list_directory(path).read() | ||
else: | ||
for index in source_files: | ||
index = os.path.join(path, index) | ||
if os.path.exists(index): | ||
base, ext = posixpath.splitext(index) | ||
ext = ext.lower() | ||
if ext == '.gz': | ||
type = 'gzipped' | ||
path = index | ||
break | ||
else: | ||
return self.list_directory(path).read() | ||
|
||
if not source_type == 'normal': | ||
type = source_type | ||
|
||
ctype = self.guess_type(path, type) | ||
print("Serving path '%s'" % 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() | ||
|
||
if type == 'normal': | ||
# 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>GUI Easy %s</title>\n" % displaypath) | ||
f.write("<body>\n<h2>Directory: %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, type): | ||
"""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 type == 'gzipped': | ||
return self.extensions_map['.gzipped'] | ||
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 | ||
'.gzipped': 'text/html', | ||
'.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 = ('localhost', 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], "..." | ||
url = 'http://localhost:' + str(sa[1]) | ||
webbrowser.open(url) | ||
httpd.serve_forever() | ||
BaseHTTPServer.test(HandlerClass, ServerClass) | ||
|
||
|
||
if __name__ == '__main__': | ||
test() |
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
Oops, something went wrong.