Skip to main content

A lightweight Python package for parsing multipart/form-data content

Project description

multipart_decoder

A lightweight Python package for parsing multipart/form-data content.

Installation

pip install multipart-decoder

Basic Usage Example for multipart_decoder

This example demonstrates how to parse simple form data with text fields.

from multipart_decoder import MultipartFormParser

# Example with raw form data
def basic_form_parsing():
    # In a real application, these would come from an HTTP request
    content_type = "multipart/form-data; boundary=----WebKitFormBoundarydVTl2EAp1scXYsWZ"
    body = "LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5ZFZUbDJFQXAxc2NYWXNXWg0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJ1c2VybmFtZSINCg0KZXhhbXBsZV91c2VyDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnlkVlRsMkVBcDFzY1hZc1daDQpDb250ZW50LURpc3Bvc2l0aW9uOiBmb3JtLWRhdGE7IG5hbWU9ImVtYWlsIg0KDQp1c2VyQGV4YW1wbGUuY29tDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnlkVlRsMkVBcDFzY1hZc1daLS0NCg=="

    # Parse the multipart form data
    form_data = MultipartFormParser(body, content_type)

    # Access the form fields
    print(f"Username: {form_data['fields']['username']}")
    print(f"Email: {form_data['fields']['email']}")

    # Show the entire parsed data structure
    print("\nComplete form data dictionary:")
    print(form_data)

if __name__ == "__main__":
    basic_form_parsing()

Expected Output

Basic Form Parsing:

  • Username: example_user
  • Email: user@example.com

Complete Form Data Dictionary:

{
    "fields": {
        "username": "example_user",
        "email": "user@example.com"
    },
    "files": {}
}

File Upload Example for multipart_decoder

This example demonstrates how to handle file uploads in multipart form data.

from multipart_decoder import MultipartFormParser
import os

def process_file_upload():
    content_type = "multipart/form-data; boundary=----WebKitFormBoundary4AQwYOUUCEUw9hsX"
    body = "LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5NEFRd1lPVVVDRVV3OWhzWA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWVsZDEiDQoNCnZhbHVlMQ0KLS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5NEFRd1lPVVVDRVV3OWhzWA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWVsZDIiDQoNCnZhbHVlMg0KLS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5NEFRd1lPVVVDRVV3OWhzWA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWxlMSI7IGZpbGVuYW1lPSJleGFtcGxlLnR4dCINCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbg0KDQpIZWxsbywgd29ybGQhDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnk0QVF3WU9VVUNFVXc5aHNYLS0NCg=="

    form_data = MultipartFormParser(body, content_type)

    # Access fields
    print(f"Field 1: {form_data['fields']['field1']}")
    print(f"Field 2: {form_data['fields']['field2']}")

    # Access files
    if 'file1' in form_data['files']:
        file_info = form_data['files']['file1']
        print(f"  - Filename: {file_info['filename']}")
        print(f"  - Content type: {file_info['content_type']}")
        print(f"  - File size: {file_info['size']} bytes")

        # Example: save the file
        save_path = f"saved_{file_info['filename']}"
        with open(save_path, "wb") as f:
            f.write(file_info['content'])
        print(f"  - File saved to: {save_path}")

        # Cleanup (for demonstration)
        os.remove(save_path)

if __name__ == "__main__":
    process_file_upload()

Expected Output:

Test Fields:

  • Field 1: value1
  • Field 2: value2

File Field:

  • Filename: example.txt
  • Content Type: text/plain
  • File Size: 13 bytes
  • File Saved To: saved_example.txt

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

multipart_decoder-0.0.0.dev0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

multipart_decoder-0.0.0.dev0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file multipart_decoder-0.0.0.dev0.tar.gz.

File metadata

  • Download URL: multipart_decoder-0.0.0.dev0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for multipart_decoder-0.0.0.dev0.tar.gz
Algorithm Hash digest
SHA256 6cccd6c35b1decaed395138cb9abe9d2554fabaa539efca839778847e58ca352
MD5 4071b4a2ac2e96079788d6ec3a542401
BLAKE2b-256 80298bd5bdc562ea266bdd0eb305a9d52d5f35dccb9d7f21a910888879cfa222

See more details on using hashes here.

File details

Details for the file multipart_decoder-0.0.0.dev0-py3-none-any.whl.

File metadata

File hashes

Hashes for multipart_decoder-0.0.0.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 e98adf059be085de9ade1775d6b7451ccac2a8e8caaa066c7310dd949f9da7ce
MD5 35da3ba81be91b1ae07be07280e3124f
BLAKE2b-256 4c9fbd910a4f130079d68b989c04043eaccdd168544a943689dacd628711d7be

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page