pamqp: AMQP 0-9-1 Low-Level Library

4.0.0 · active · verified Thu Apr 09

pamqp is a low-level Python library for marshaling and unmarshaling AMQP 0-9-1 protocol frames. It provides programmatic access to AMQP commands, headers, and body frames, allowing users to build clients, servers, or proxies that interact directly with the AMQP protocol, notably for RabbitMQ. The current version is 4.0.0, with stable releases typically occurring to address Python compatibility or critical bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create AMQP command, header, and body objects, and then marshal them into raw AMQP frames using `pamqp.frame.Frame.marshal`. It also shows a simple unmarshal operation for a single frame. Note that `pamqp` only handles serialization; managing network I/O and sequencing of frames into complete messages is left to the user.

import pamqp.frame
import pamqp.commands.basic
import pamqp.header
import pamqp.body

# Example: Prepare a Basic.Publish command and marshal it into frames
channel_id = 1
exchange = 'my_exchange'
routing_key = 'my_key'
payload = b'Hello, pamqp!'

# 1. Create the Basic.Publish command object
publish_cmd = pamqp.commands.basic.Publish(
    exchange=exchange,
    routing_key=routing_key,
    mandatory=False,
    immediate=False
)

# 2. Create ContentHeader for the message properties
header = pamqp.header.ContentHeader(
    class_id=publish_cmd.name_id, # class_id comes from the command
    body_size=len(payload),
    properties={} # Basic properties for this example
)

# 3. Create ContentBody for the actual message payload
body = pamqp.body.ContentBody(value=payload)

# 4. Marshal the command into an AMQP frame
command_frame_bytes = pamqp.frame.Frame.marshal(publish_cmd, channel_id)

# 5. Marshal the content header into an AMQP frame
header_frame_bytes = pamqp.frame.Frame.marshal(header, channel_id)

# 6. Marshal the content body into an AMQP frame
body_frame_bytes = pamqp.frame.Frame.marshal(body, channel_id)

print(f"Marshalled command frame: {command_frame_bytes.hex()}")
print(f"Marshalled header frame: {header_frame_bytes.hex()}")
print(f"Marshalled body frame: {body_frame_bytes.hex()}")

# To demonstrate unmarshalling a single frame (e.g., a command frame)
unmarshalled_cmd_frame = pamqp.frame.Frame.unmarshal(command_frame_bytes)
print(f"\nUnmarshalled frame type: {unmarshalled_cmd_frame.frame_type_id}")
print(f"Unmarshalled channel ID: {unmarshalled_cmd_frame.channel_id}")
print(f"Unmarshalled command: {unmarshalled_cmd_frame.value.__class__.__name__}")

view raw JSON →