Pyjo.Content.Single - HTTP content¶
from Pyjo.Content.Single import Single
single = Single()
single.parse(b'Content-Length: 12\x0d\x0a\x0d\x0aHello World!')
print(single.headers.content_length)
# Or import the module, as in Google Python Style Guide
import Pyjo.Content.Single
single = Pyjo.Content.Single.new()
single.parse(b'Content-Length: 12\x0d\x0a\x0d\x0aHello World!')
print(single.headers.content_length)
Pyjo.Content.Single is a container for HTTP content, based on
RFC 7230 and
RFC 7231.
Events¶
Pyjo.Content.Single inherits all events from Pyjo.Content and
can emit the following new ones.
upgrade¶
single.on('upgrade', lambda single, multi: ...)
Emitted when content gets upgraded to a Pyjo.Content.MultiPart
object.
@single.on('upgrade')
def upgrade(single, multi):
if m := re.search(r'multipart/([^;]+)', multi.headers.content_type, re.I):
print(f'Multipart: {m[1]}')
See also¶
Pyjoyment, https://mojolicious.org.
Ported from Mojo::Content::Single of Mojolicious 9.49.
- class Pyjo.Content.Single.Pyjo_Content_Single(*, asset: Pyjo_Asset | None = None, auto_upgrade: bool = True, **kwargs: Any)[source]¶
Pyjo.Content.Singleinherits all methods fromPyjo.Contentand implements the following new ones.- __init__(*, asset: Pyjo_Asset | None = None, auto_upgrade: bool = True, **kwargs: Any) None[source]¶
single = Pyjo.Content.Single.new() single = Pyjo.Content.Single.new(asset=Pyjo.Asset.File.new())
Construct a new
Pyjo.Content.Singleobject and subscribe to eventreadwith default content parser.
- property asset: Pyjo_Asset[source]¶
asset = single.asset single.asset = Pyjo.Asset.Memory.new()
The actual content, defaults to a
Pyjo.Asset.Memoryobject withPyjo.Asset.Memory.Pyjo_Asset_Memory.auto_upgradeenabled.
- auto_upgrade¶
boolean = single.auto_upgrade single.auto_upgrade = boolean
Try to detect multipart content and automatically upgrade to a
Pyjo.Content.MultiPartobject, defaults to a true value.
- body_contains(chunk: bytes) bool[source]¶
boolean = single.body_contains(b'1234567')
Check if content contains a specific string.
- clone() Self | None[source]¶
clone = single.clone()
Return a new
Pyjo.Content.Singleobject cloned from this content if possible, otherwise returnNone.
- get_body_chunk(offset: int) bytes | None[source]¶
chunk = single.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.
- parse(chunk: bytes | None = None) Pyjo_Content[source]¶
single = single.parse(b'Content-Length: 12\x0d\x0a\x0d\x0aHello World!') multi = single.parse(b'Content-Type: multipart/form-data\x0d\x0a\x0d\x0a')
Parse content chunk and upgrade to
Pyjo.Content.MultiPartobject if necessary.