You may have solved it by now, but I did play with it a few months ago
and worked out a proof-of-concept file upload monitor application. I
got rid of some paste.progress annoyances with my own Monitor
middleware extending paste.progress.UploadProgressMonitor.

import time
from decimal import Decimal

from paste.progress import _ProgressFile, UploadProgressMonitor,
from paste.progress import ENVIRON_RECEIVED, REQUEST_STARTED,
from pylons.wsgiapp import PylonsBaseWSGIApp
from pylons import config

# testing emulated bandwidth
KBPS = 'progress.kbps'

class ProgressFile(_ProgressFile):

    def __init__(self, environ, rfile):
        self._ProgressFile_environ = environ
        self._ProgressFile_rfile = rfile

        if config.get('debug', False):
            kbps = config.get(KBPS, KBPS_DEFAULT)
            self.bps = Decimal(kbps) * 1024

    def readline(self, hint=None):
        if hint is None:
            hint = -1
        chunk = self._ProgressFile_rfile.readline(hint)
        if hasattr(self, 'bps'):
            time.sleep(len(chunk) / self.bps)
        self._ProgressFile_environ[ENVIRON_RECEIVED] += len(chunk)
        return chunk

class Monitor(UploadProgressMonitor):

    def __call__(self, environ, start_response):
        length = environ.get('CONTENT_LENGTH', 0)
        if length and int(length) > self.threshold:
            # replace input file object
            environ[ENVIRON_RECEIVED] = 0
            environ[REQUEST_STARTED] = time.time()
            environ[REQUEST_FINISHED] = None
            environ['wsgi.input'] = ProgressFile(environ,
            def finalizer(exc_info=None):
                environ[REQUEST_FINISHED] = time.time()
            return catch_errors(self.application, environ,
start_response, finalizer, finalizer)
        return self.application(environ, start_response)

Never tested with newer releases of Pylons and related dependencies,
but please contact me if you still get errors or want to see the
sample app.
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at

Reply via email to