安装 paste 后直接执行便可!

现已加入 google code 中

   1 from paste.progress import *
   2 import time
   3 
   4 upload_page = '''
   5 <html>
   6     <head>
   7         <script type="text/javascript">
   8             // Tested with Opera 9.01, Firefox beta 2 on Linux, should
   9             // work with IE 6+
  10 
  11             poller = false;
  12             busy = false;
  13             http = false;
  14 
  15             function getstats() {
  16                 // Get our XMLHttpRequest object and submit request for stats
  17                 if(!busy){
  18                     http = false;
  19                     if(window.XMLHttpRequest) {
  20                         http = new XMLHttpRequest();
  21                     }
  22                     else if(window.ActiveXObject) {
  23                         try {
  24                             http = new ActiveXObject("Msxml2.XMLHTTP");
  25                         } catch (e) {
  26                             try {
  27                                 http = new ActiveXObject("Microsoft.XMLHTTP");
  28                             } catch (e) {}
  29                         }
  30                     }
  31                     if(!http) {
  32                         alert("We're uploading the file, but your browser isn't letting us use AJAX.");
  33                         document.getElementById('upload_stat').innerHTML='Not available.';
  34                         return false;
  35                     }
  36 
  37                     busy = true;
  38                     http.onreadystatechange = update_stat;
  39                     http.open("GET", "/report", true);
  40                     http.send(null);
  41                 }
  42             }
  43 
  44             function update_stat(){
  45                 var result;
  46                 // Handles the request for stats.
  47                 if(http.readyState == 4){
  48                     if(http.status != 200){
  49                        document.getElementById('upload_stat').innerHTML='Stats unavailable. Wait for it.';
  50                         clearInterval(poller);
  51                     }else{
  52                         result = eval(http.responseText)
  53                         var current = result[result.length-1]
  54                         received = current.bytes_received
  55                         size = current.content_length
  56                         document.getElementById('upload_stat').innerHTML=100*received/size + "%";
  57                         if(http.responseText.indexOf('No active transfers!') != -1){
  58                             clearInterval(poller);
  59                         }
  60                     }
  61                 }
  62                 busy = false;
  63             }
  64             function start_stat(){
  65                 // Hides our upload form and displays our stats element.
  66                 poller = setInterval("getstats()", 2000);
  67                 document.getElementById('upload_stat').style.display = 'block';
  68                 document.getElementById('upload_form').style.display = 'none';
  69             }
  70         </script>
  71     </head>
  72     <bod>
  73         <!-- Our form will call the polling system for stats when it's submitted, as well as start the uploading of the file. -->
  74         <h1>File upload with progress bar</h1>
  75         Choose your files:<br/>
  76         <form class="upload_form" action="" method="post" enctype="multipart/form-data" onsubmit="start_stat()">
  77             <input type="file" name="myFile"/>
  78             <input type="submit" value="Upload File"/>
  79         </form>
  80         <!-- This is where we'll be putting our statistics. It's hidden to start off. -->
  81         <div id="upload_stat" style="display:none;">Connecting...</div>
  82     </body>
  83 </html>'''
  84 
  85 def FileUploadApp(environ, start_response):
  86     total = environ.get('CONTENT_LENGTH')
  87     if total:
  88         body = '''
  89         <html><head><title>Upload Succed!</title></head>
  90         <body><h5>%s bytes upload succed!</h5></body></html>
  91         ''' % total
  92     else:
  93         body = upload_page
  94 
  95     start_response('200 OK', [('Content-Type','text/html'),
  96                               ('Content-Length',len(body))])
  97     return [body]
  98 
  99 class FileUploader(object):
 100     def __init__(self, app):
 101         self.chunk_size = 4096
 102         self.delay = 1
 103         self.progress = True
 104         self.app = app
 105     def __call__(self, environ, start_response):
 106         size = 0
 107         total  = environ.get('CONTENT_LENGTH')
 108         if total:
 109             remaining = int(total)
 110             while remaining > 0:
 111                 if self.progress:
 112                     print "%s of %s remaining" % (remaining, total)
 113                 if remaining > 4096:
 114                     chunk = environ['wsgi.input'].read(4096)
 115                 else:
 116                     chunk = environ['wsgi.input'].read(remaining)
 117                 if not chunk:
 118                     break
 119                 size += len(chunk)
 120                 remaining -= len(chunk)
 121                 if self.delay:
 122                     time.sleep(self.delay)
 123         print "bingles"
 124         return self.app(environ, start_response)
 125 
 126 if __name__ == '__main__':
 127     from paste.httpserver import serve
 128     from paste.urlmap import URLMap
 129     from paste.auth.basic import AuthBasicHandler
 130     realm = 'Test Realm'
 131     def authfunc(environ, username, password):
 132         return username == password
 133 
 134     map = URLMap({})
 135     ups = UploadProgressMonitor(map, threshold=1024, timeout=0)
 136     map['/upload'] = FileUploader(FileUploadApp)
 137     map['/report'] = UploadProgressReporter(ups)
 138     serve(AuthBasicHandler(ups, realm, authfunc))