circuits.web.parsers.multipart module

Parser for multipart/form-data

This module provides a parser for the multipart/form-data format. It can read from a file, a socket or a WSGI environment. The parser can be used to replace cgi.FieldStorage (without the bugs) and works with Python 2.5+ and 3.x (2to3).

Licence (MIT)

Copyright (c) 2010, Marcel Hellkamp. Inspired by the Werkzeug library: http://werkzeug.pocoo.org/

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

class circuits.web.parsers.multipart.MultiDict(*a, **k)

Bases: MutableMapping

A dict that remembers old values for each key

keys() a set-like object providing a view on D's keys
append(key, value)
replace(key, value)
getall(key)
get(k[, d]) D[k] if k in D, else d.  d defaults to None.
iterallitems()
circuits.web.parsers.multipart.tob(data, enc='utf8')
circuits.web.parsers.multipart.copy_file(stream, target, maxread=-1, buffer_size=32)

Read from :stream and write to :target until :maxread or EOF.

circuits.web.parsers.multipart.header_quote(val)
circuits.web.parsers.multipart.header_unquote(val, filename=False)
circuits.web.parsers.multipart.parse_options_header(header, options=None)
exception circuits.web.parsers.multipart.MultipartError

Bases: ValueError

class circuits.web.parsers.multipart.MultipartParser(stream, boundary, content_length=-1, disk_limit=1073741824, mem_limit=1048576, memfile_limit=262144, buffer_size=65536, charset='latin1')

Bases: object

Parse a multipart/form-data byte stream. This object is an iterator over the parts of the message.

Parameters
  • stream – A file-like stream. Must implement .read(size).

  • boundary – The multipart boundary as a byte string.

  • content_length – The maximum number of bytes to read.

parts()

Returns a list with all parts of the multipart message.

get(name, default=None)

Return the first part with that name or a default value (None).

get_all(name)

Return a list of parts with that name.

class circuits.web.parsers.multipart.MultipartPart(buffer_size=65536, memfile_limit=262144, charset='latin1')

Bases: object

feed(line, nl='')
write_header(line, nl)
write_body(line, nl)
finish_header()
is_buffered()

Return true if the data is fully buffered in memory.

property value

Data decoded with the specified charset

save_as(path)
circuits.web.parsers.multipart.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 (text_type) and lists as values (multiple values per key are possible). The forms-dictionary contains form-field values as text_type strings. The files-dictionary contains MultipartPart instances, either because the form-field was a file-upload or the value is to big to fit into memory limits.

Parameters
  • environ – An WSGI environment dict.

  • charset – The charset to use if unsure. (default: utf8)

  • strict – If True, raise MultipartError on any parsing errors. These are silently ignored by default.