## page was renamed from file upload monitor 安装 [[http://pythonpaste.org/|paste]] 后直接执行便可! [[http://huangyilib.googlecode.com/svn/trunk/file_upload_monitor.py|现已加入 google code 中]] {{{#!python from paste.progress import * import time upload_page = '''

File upload with progress bar

Choose your files:
''' def FileUploadApp(environ, start_response): total = environ.get('CONTENT_LENGTH') if total: body = ''' Upload Succed!
%s bytes upload succed!
''' % total else: body = upload_page start_response('200 OK', [('Content-Type','text/html'), ('Content-Length',len(body))]) return [body] class FileUploader(object): def __init__(self, app): self.chunk_size = 4096 self.delay = 1 self.progress = True self.app = app def __call__(self, environ, start_response): size = 0 total = environ.get('CONTENT_LENGTH') if total: remaining = int(total) while remaining > 0: if self.progress: print "%s of %s remaining" % (remaining, total) if remaining > 4096: chunk = environ['wsgi.input'].read(4096) else: chunk = environ['wsgi.input'].read(remaining) if not chunk: break size += len(chunk) remaining -= len(chunk) if self.delay: time.sleep(self.delay) print "bingles" return self.app(environ, start_response) if __name__ == '__main__': from paste.httpserver import serve from paste.urlmap import URLMap from paste.auth.basic import AuthBasicHandler realm = 'Test Realm' def authfunc(environ, username, password): return username == password map = URLMap({}) ups = UploadProgressMonitor(map, threshold=1024, timeout=0) map['/upload'] = FileUploader(FileUploadApp) map['/report'] = UploadProgressReporter(ups) serve(AuthBasicHandler(ups, realm, authfunc)) }}}