Did I find the right examples for you? yes no

All Samples(6)  |  Call(6)  |  Derive(0)  |  Import(0)
Parse form data from an environ dict and return a (forms, files) tuple.
Both tuple values are dictionaries with the form-field name as a key
(unicode) and lists as values (multiple values per key are possible).
The forms-dictionary contains form-field values as unicode strings.
The files-dictionary contains :class:`MultipartPart` instances, either
because the form-field was a file-upload or the value is to big to fit
into memory limits.

:param environ: An WSGI environment dict.
:param charset: The charset to use if unsure. (default: utf8)(more...)

        def parse_form_data(environ, charset='utf8', strict=False, **kw):
    ''' Parse form data from an environ dict and return a (forms, files) tuple.
        Both tuple values are dictionaries with the form-field name as a key
        (unicode) and lists as values (multiple values per key are possible).
        The forms-dictionary contains form-field values as unicode strings.
        The files-dictionary contains :class:`MultipartPart` instances, either
        because the form-field was a file-upload or the value is to big to fit
        into memory limits.
        
        :param environ: An WSGI environment dict.
        :param charset: The charset to use if unsure. (default: utf8)
        :param strict: If True, raise :exc:`MultipartError` on any parsing
                       errors. These are silently ignored by default.
    '''
        
    forms, files = MultiDict(), MultiDict()
    try:
        if environ.get('REQUEST_METHOD','GET').upper() not in ('POST', 'PUT'):
            raise MultipartError("Request method other than POST or PUT.")
        content_length = int(environ.get('CONTENT_LENGTH', '-1'))
        content_type = environ.get('CONTENT_TYPE', '')
        if not content_type:
            raise MultipartError("Missing Content-Type header.")
        content_type, options = parse_options_header(content_type)
        stream = environ.get('wsgi.input') or BytesIO()
        kw['charset'] = charset = options.get('charset', charset)
        if content_type == 'multipart/form-data':
            boundary = options.get('boundary','')
            if not boundary:
                raise MultipartError("No boundary for multipart/form-data.")
            for part in MultipartParser(stream, boundary, content_length, **kw):
                if part.filename or not part.is_buffered():
                    files[part.name] = part
                else: # TODO: Big form-fields are in the files dict. really?
                    forms[part.name] = part.value
        elif content_type in ('application/x-www-form-urlencoded',
                              'application/x-url-encoded'):
            mem_limit = kw.get('mem_limit', 2**20)
            if content_length > mem_limit:
                raise MultipartError("Request to big. Increase MAXMEM.")
            data = stream.read(mem_limit).decode(charset)
            if stream.read(1): # These is more that does not fit mem_limit
                raise MultipartError("Request to big. Increase MAXMEM.")
            data = parse_qs(data, keep_blank_values=True)
            for key, values in data.iteritems():
                for value in values:
                    forms[key] = value
        else:
            raise MultipartError("Unsupported content type.")
    except MultipartError:
        if strict: raise
    return forms, files
        


src/m/u/multipart-0.1/test/test_multipart.py   multipart(Download)
        kwargs['strict'] = True
        kwargs['charset'] = 'utf8'
        return mp.parse_form_data(**kwargs)
 
    def test_multipart(self):
        kwargs['strict'] = True
        kwargs['charset'] = 'utf8'
        return mp.parse_form_data(**kwargs)
 
    def assertMPError(self, *a, **ka):
                   'CONTENT_TYPE': 'multipart/form-data; boundary=%s'%boundary,
                   'wsgi.input': BytesIO(browser_test_cases[name]['data'])}
            rforms, rfiles = mp.parse_form_data(env, strict=True, charset='utf8')
            for field in files:
                self.assertEqual(rfiles[field].name, field)

src/m/u/multipart-HEAD/test/test_multipart.py   multipart(Download)
        kwargs['strict'] = True
        kwargs['charset'] = 'utf8'
        return mp.parse_form_data(**kwargs)
 
    def test_multipart(self):
        kwargs['strict'] = True
        kwargs['charset'] = 'utf8'
        return mp.parse_form_data(**kwargs)
 
    def assertMPError(self, *a, **ka):
                   'CONTENT_TYPE': 'multipart/form-data; boundary=%s'%boundary,
                   'wsgi.input': BytesIO(browser_test_cases[name]['data'])}
            rforms, rfiles = mp.parse_form_data(env, strict=True, charset='utf8')
            for field in files:
                self.assertEqual(rfiles[field].name, field)