CurlMime Objects¶
PycURL exposes libcurl’s MIME tree API via CurlMime and CurlMimePart classes.
CurlMime offers both low-level wrappers and higher-level builder helpers:
Low-level:
addpart()andCurlMimePartmethods such asname(),data(),data_cb(),filedata()andsubparts().Builder helpers:
add(),add_field(),add_file()andadd_multipart().CurlMimePart.data()also accepts objects exposing Python’s buffer protocol (for examplebytearrayandmemoryview), not onlybytes/ASCIIstr.CurlMimePart.data_cb(datasize, read, seek=None, free=None, userdata=None)maps to libcurlcurl_mime_data_cb()for streaming content from callbacks.read(userdata, max_bytes)uses the same return conventions asREADFUNCTION. Optionalseek(userdata, offset, origin)followsSEEKFUNCTIONsemantics and should returnSEEKFUNC_OK,SEEKFUNC_FAILorSEEKFUNC_CANTSEEK. Optionalfree(userdata)is called when libcurl releases the callback-backed part.
Example:
import pycurl
curl = pycurl.Curl()
mime = pycurl.CurlMime(curl)
mime.add_field("field1", "value1")
mime.add_file("upload", "/tmp/example.txt", content_type="text/plain")
nested = mime.add_multipart(name="attachments", subtype="mixed")
nested.add_field("meta", "nested-value")
Note
This is a first-draft API. It currently documents MIME object construction and nesting. End-to-end request attachment via Curl options is documented separately as that integration is finalized.
Note
Ownership and handle constraints:
CurlMimeobjects passed toCurl.setopt(pycurl.MIMEPOST, mime)must be top-level/owning MIME trees (not already attached viasubparts()).CurlMimePart.subparts(child)requires bothCurlMimeobjects to use the sameCurlhandle.A
CurlMimecurrently set asMIMEPOSTcannot be attached assubparts().Curl.duphandle()duplicates callback-backed MIME parts and shares the callbackuserdatapointer between handles, matching libcurl behavior.
CurlMime¶
- class pycurl.CurlMime¶
Python wrapper for libcurl MIME API.
CurlMime objects have the following methods:
- close()¶
Release the underlying curl_mime handle.
- closed()¶
Return whether this CurlMime object is closed.
- addpart()¶
Create and return a new MIME part.
- add()¶
Add a part using a keyword-oriented builder API.
- add_field()¶
Add a simple form field part.
- add_file()¶
Add a file upload part.
- add_multipart()¶
Add and attach a nested multipart CurlMime.
CurlMimePart¶
- class pycurl.CurlMimePart¶
A MIME part belonging to a CurlMime object.
CurlMimePart objects have the following methods:
- name(object, /)¶
Set the name of this MIME part.
- data(object, /)¶
Set in-memory data for this MIME part.
- data_cb()¶
Set callback-based data for this MIME part.
- filedata(object, /)¶
Set on-disk file data for this MIME part.
- filename(object, /)¶
Set the remote filename for this MIME part.
- type(object, /)¶
Set content type for this MIME part.
- encoder(object, /)¶
Set content transfer encoding for this MIME part.
- headers(object, /)¶
Set custom headers for this MIME part.
- subparts(object, /)¶
Attach a child CurlMime object as multipart data.