Mercurial > hg > FileServer
comparison fileserver/web.py @ 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 |
comparison
equal
deleted
inserted
replaced
0:8d31e36f084e | 1:89d4f742ed1a |
---|---|
6 Reference: | 6 Reference: |
7 - http://docs.webob.org/en/latest/file-example.html | 7 - http://docs.webob.org/en/latest/file-example.html |
8 """ | 8 """ |
9 | 9 |
10 import mimetypes | 10 import mimetypes |
11 import optparse | |
11 import os | 12 import os |
13 import sys | |
12 from webob import Request, Response, exc | 14 from webob import Request, Response, exc |
15 from wsgiref.simple_server import make_server | |
16 | |
17 __all__ = ['get_mimetype', 'file_response', 'FileApp', 'DirectoryServer'] | |
13 | 18 |
14 def get_mimetype(filename): | 19 def get_mimetype(filename): |
15 type, encoding = mimetypes.guess_type(filename) | 20 type, encoding = mimetypes.guess_type(filename) |
16 # We'll ignore encoding, even though we shouldn't really | 21 # We'll ignore encoding, even though we shouldn't really |
17 return type or 'application/octet-stream' | 22 return type or 'application/octet-stream' |
73 # '/' from both sides; so we correct here | 78 # '/' from both sides; so we correct here |
74 return exc.HTTPNotFound()(environ, start_response) | 79 return exc.HTTPNotFound()(environ, start_response) |
75 response = file_response(full) | 80 response = file_response(full) |
76 return response(environ, start_response) | 81 return response(environ, start_response) |
77 | 82 |
83 def main(args=sys.argv[1:]): | |
84 | |
85 # parse command line arguments | |
86 usage = '%prog [options] directory' | |
87 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): | |
88 """description formatter""" | |
89 def format_description(self, description): | |
90 if description: | |
91 return description + '\n' | |
92 else: | |
93 return '' | |
94 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) | |
95 parser.add_option('-p', '--port', dest='port', | |
96 type='int', default=9999, | |
97 help='port [DEFAULT: %default]') | |
98 parser.add_option('-H', '--host', dest='host', default='0.0.0.0', | |
99 help='host [DEFAULT: %default]') | |
100 options, args = parser.parse_args(args) | |
101 | |
102 # get the directory | |
103 if not len(args) == 1: | |
104 parser.print_help() | |
105 sys.exit(1) | |
106 directory = args[0] | |
107 if not os.path.exists(directory): | |
108 parser.error("'%s' not found" % directory) | |
109 if not os.path.isdir(directory): | |
110 parser.error("'%s' not a directory" % directory) | |
111 | |
112 # serve | |
113 app = DirectoryServer(directory) | |
114 try: | |
115 print 'http://%s:%s/' % (options.host, options.port) | |
116 make_server(options.host, options.port, app).serve_forever() | |
117 except KeyboardInterrupt, ki: | |
118 print "Cio, baby!" | |
119 except BaseException, e: | |
120 sys.exit("Problem initializing server: %s" % e) | |
121 | |
78 if __name__ == '__main__': | 122 if __name__ == '__main__': |
79 from wsgiref import simple_server | 123 main() |
80 app = Handler() | |
81 server = simple_server.make_server(host='0.0.0.0', port=8080, app=app) | |
82 server.serve_forever() |