pamqp: AMQP 0-9-1 Low-Level Library
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
- breaking Version 4.0.0 and later require Python 3.11 or newer. Previous versions supported Python 3.7+.
- breaking In v4.0.0, explicit `__annotations__` class variables were removed from command and base classes. If your code directly introspects `MyCommand.__annotations__`, it may need updating as Python now manages this automatically.
- breaking Major module reorganization occurred in version 3.x. AMQP command classes (e.g., `Basic.Publish`), exceptions, and constants were moved from `pamqp.specification` to dedicated modules like `pamqp.commands.*`, `pamqp.exceptions`, and `pamqp.constants`.
- breaking The `Basic.QoS` command's `global_` argument was renamed to `globally` in version 3.0.0a5.
- gotcha pamqp is a low-level AMQP 0-9-1 protocol serialization library. It does not handle network I/O, connection management, threading, or application logic. Users must implement these aspects themselves to build a functional AMQP client or server.
- gotcha In `pamqp` versions < 3.0.1, the `Basic.Reject(requeue=False)` command parameter was erroneously coerced to `True`, meaning messages were always requeued. While fixed in 3.0.1 and later (including 4.0.0), this highlights the importance of verifying critical parameter behavior.
Install
-
pip install pamqp
Imports
- Frame
from pamqp.frame import Frame
- Publish
from pamqp.commands.basic import Publish
- ContentHeader
from pamqp.header import ContentHeader
- ContentBody
from pamqp.body import ContentBody
- AMQPConnectionError
from pamqp.exceptions import AMQPConnectionError
Quickstart
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__}")