# HG changeset patch # User Jeff Hammel # Date 1330550236 28800 # Node ID 89d4f742ed1ae0e6fe18af8d1e248f59f012053f # Parent 8d31e36f084e9fa0796c8333b006db1498b3ca36 include cli front end diff -r 8d31e36f084e -r 89d4f742ed1a fileserver/__init__.py --- 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 * diff -r 8d31e36f084e -r 89d4f742ed1a fileserver/web.py --- 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()