Pyjo.Content.MultiPart - HTTP multipart content

from Pyjo.Content.MultiPart import MultiPart

multi = MultiPart()
multi.parse(b'Content-Type: multipart/mixed; boundary=---foobar')
single = multi.parts[4]

# Or import the module, as in Google Python Style Guide

import Pyjo.Content.MultiPart

multi = Pyjo.Content.MultiPart.new()
multi.parse(b'Content-Type: multipart/mixed; boundary=---foobar')
single = multi.parts[4]

Pyjo.Content.MultiPart is a container for HTTP multipart content, based on RFC 7230, RFC 7231 and RFC 2388.

Events

Pyjo.Content.MultiPart inherits all events from Pyjo.Content and can emit the following new ones.

part

multi.on('part', lambda multi, single: ...)

Emitted when a new Pyjo.Content.Single part starts, before its headers have been parsed.

@multi.on('part')
def part(multi, single):
    @single.on('body')
    def body(single):
        if m := re.search(r'name="([^"]+)"', single.headers.content_disposition or ''):
            print(f'Field: {m[1]}')

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojo::Content::MultiPart of Mojolicious 9.49.

class Pyjo.Content.MultiPart.Pyjo_Content_MultiPart(*, parts: list[Pyjo_Content] | None = None, **kwargs: Any)[source]

Pyjo.Content.MultiPart inherits all methods from Pyjo.Content and implements the following new ones.

__init__(*, parts: list[Pyjo_Content] | None = None, **kwargs: Any) → None[source]
multi = Pyjo.Content.MultiPart.new()
multi = Pyjo.Content.MultiPart.new(parts=[Pyjo.Content.Single.new()])

Construct a new Pyjo.Content.MultiPart object and subscribe to event read with default content parser.

body_contains(chunk: bytes) → bool[source]
boolean = multi.body_contains(b'foobarbaz')

Check if content parts contain a specific string.

property body_size: int[source]
size = multi.body_size

Content size in bytes.

build_boundary() → str[source]
boundary = multi.build_boundary()

Generate a suitable boundary for content and add it to Content-Type header.

clone() → Self | None[source]
clone = multi.clone()

Return a new Pyjo.Content.MultiPart object cloned from this content if possible, otherwise return None.

get_body_chunk(offset: int) → bytes | None[source]
chunk = multi.get_body_chunk(0)

Get a chunk of content starting from a specific position. Note that it might not be possible to get the same chunk twice if content was generated dynamically.

property is_multipart: bool[source]
boolean = multi.is_multipart

True, this is a Pyjo.Content.MultiPart object.

parts: list[Pyjo_Content]
parts = multi.parts
multi.parts = [Pyjo.Content.Single.new()]

Content parts embedded in this multipart content, usually Pyjo.Content.Single objects.