Poster3

0.8.1 · maintenance · verified Thu Apr 16

Poster3 (version 0.8.1) is a Python library providing support for streaming HTTP POST requests and multipart/form-data encoding. It's a fork of the original 'poster' package, designed to address the limitation in Python's standard library where large files had to be loaded entirely into memory before uploading. The library allows for efficient uploading of large files without excessive memory consumption. Its last release was in December 2018, indicating a maintenance-level release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `poster3` to upload a file along with other form data using `urllib.request`. It registers the streaming HTTP handlers, encodes the multipart data, and then sends the request. Remember to replace `POSTER3_TEST_URL` with your actual endpoint.

import os
import urllib.request
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers

# Register the streaming http handlers with urllib2
register_openers()

# Prepare file and simple field for upload
filename = 'test_upload.txt'
with open(filename, 'w') as f:
    f.write('This is a test file for poster3 upload.')

# 'datagen' is a generator object that yields ranges of the encoded multipart data
# 'headers' is a dictionary of headers that must be included with the request
datagen, headers = multipart_encode({"field1": "value1", "file1": open(filename, "rb")})

# In a real scenario, you'd replace this with your actual upload URL
upload_url = os.environ.get('POSTER3_TEST_URL', 'http://httpbin.org/post')

# Create the request object
request = urllib.request.Request(upload_url, datagen, headers)

# Actually upload the file
print(f"Uploading to {upload_url}...")
try:
    response = urllib.request.urlopen(request)
    print("Upload successful!")
    print(response.read().decode('utf-8'))
except urllib.error.URLError as e:
    print(f"Upload failed: {e.reason}")
finally:
    # Clean up the test file
    os.remove(filename)

view raw JSON →