Mercurial > hg > FileServer
changeset 1:89d4f742ed1a
include cli front end
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 29 Feb 2012 13:17:16 -0800 |
parents | 8d31e36f084e |
children | 8fb047af207a |
files | fileserver/__init__.py fileserver/web.py |
diffstat | 2 files changed, 46 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/fileserver/__init__.py Wed Feb 29 13:09:11 2012 -0800 +++ b/fileserver/__init__.py Wed Feb 29 13:17:16 2012 -0800 @@ -1,2 +1,2 @@ -# +from web import *
--- a/fileserver/web.py Wed Feb 29 13:09:11 2012 -0800 +++ b/fileserver/web.py Wed Feb 29 13:17:16 2012 -0800 @@ -8,8 +8,13 @@ """ import mimetypes +import optparse import os +import sys from webob import Request, Response, exc +from wsgiref.simple_server import make_server + +__all__ = ['get_mimetype', 'file_response', 'FileApp', 'DirectoryServer'] def get_mimetype(filename): type, encoding = mimetypes.guess_type(filename) @@ -75,8 +80,44 @@ response = file_response(full) return response(environ, start_response) +def main(args=sys.argv[1:]): + + # parse command line arguments + usage = '%prog [options] directory' + class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): + """description formatter""" + def format_description(self, description): + if description: + return description + '\n' + else: + return '' + parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) + parser.add_option('-p', '--port', dest='port', + type='int', default=9999, + help='port [DEFAULT: %default]') + parser.add_option('-H', '--host', dest='host', default='0.0.0.0', + help='host [DEFAULT: %default]') + options, args = parser.parse_args(args) + + # get the directory + if not len(args) == 1: + parser.print_help() + sys.exit(1) + directory = args[0] + if not os.path.exists(directory): + parser.error("'%s' not found" % directory) + if not os.path.isdir(directory): + parser.error("'%s' not a directory" % directory) + + # serve + app = DirectoryServer(directory) + try: + print 'http://%s:%s/' % (options.host, options.port) + make_server(options.host, options.port, app).serve_forever() + except KeyboardInterrupt, ki: + print "Cio, baby!" + except BaseException, e: + sys.exit("Problem initializing server: %s" % e) + if __name__ == '__main__': - from wsgiref import simple_server - app = Handler() - server = simple_server.make_server(host='0.0.0.0', port=8080, app=app) - server.serve_forever() + main()