RabbitMQ asynchronous connector library for Python with built in RPC support

Overview

badge PyPI version fury.io codecov PyPI download month PyPI pyversions Ask Me Anything !

About

RabbitMQ connector library for Python that is fully integrated with the aio-pika framework.

Introduction

BunnyStorm is here to simplify working with RabbitMQ while using aio-pika. This library offers an asynchronous implementation of a RabbitMQ connector which is fully integrated with asyncio. BunnyStorm provides an all-in-one adapter with the following functionalities:

  1. publish - Publish a message.
  2. receive - Consume messages from a queue. Can automatically reply to desired routes if the received message contains a "reply_to" property.
  3. rpc - Implement RPC (Remote procedure call) logic using RabbitMQ. Publish a message with a reply_to property, wait for a reply message and return the reply's content.

Installation

pip install -U bunny_storm

Examples

Simple Receiver (print messages from queue)

import asyncio
from bunny_storm import AsyncAdapter, RabbitMQConnectionData

RABBIT_URI = "amqp://guest:[email protected]:5672/"

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    configuration = dict(
        publish=dict(
            exchange="some_ex",
            exchange_type="direct",
            routing_key="some_rk",
        ),
        receive=dict(
            exchange="some_receive_ex",
            exchange_type="direct",
            routing_key="some_rk",
            queue="some_q",
        )
    )
    connection_data = RabbitMQConnectionData(username="guest", password="guest", connection_name="example")
    adapter = AsyncAdapter(rabbitmq_connection_data=connection_data, configuration=configuration, loop=loop)
    loop.create_task(adapter.receive(handler=lambda msg: print(msg.body), queue="some_q"))
    loop.run_forever()

Full Microservices Using RPC pattern

Example of 2 Microservices implementing a fully scalable application that calculates a number in the Fibonacci series while implementing RabbitMQ Remote procedure call (RPC) pattern, can be found at the examples directory.

Class Diagram

Class Diagram

Architecture

  1. RabbitMQConnectionData - A simple dataclass which contains all the relevant credentials and parameters necessary for opening a connection to a RabbitMQ server.

  2. AsyncConnection - A class responsible for the management of a single connection to a RabbitMQ server. The class connects to a server whose credentials are specified in a RabbitMQConnectionData object passed to it. The main function of this class is get_connection which uses aio-pika to open a robust connection to the server.

  3. ChannelConfiguration - A class which manages a single channel within a given RabbitMQ connection. This class encapsulates an AsyncConnection object, and exposes functionality to declare exchanges and queues. This class receives a connection (AsyncConnection from the previous paragraph) and parameters relevant to the creation of the channel.

  4. Publisher - A class which creates and uses a ChannelConfiguration object to publish messages to a given exchange. Automatically declares the desired exchange with various configurable parameters, such as exchange type. The main function of this class is publish, which ensures that the instance's channel is open and that the relevant exchange has been declared, following which, it publishes a message to the exchange.

  5. Consumer - A class which creates and uses a ChannelConfiguration object to consume messages from a given queue. Automatically declares the desired queue, and optionally an exchange as well, with various configurable parameters. The main functionality of this class is consume, which ensures that the instance's channel is open and that the relevant queue and exchange have been declared and bound as desired, following which, it consumes messages from the queue.

  6. AsyncAdapter - A class which exposes all the desired functionality for this framework:

    1. publish: Publish a message to a given exchange.
    2. receive: Receive messages from a given queue. Messages received which have their reply_to parameter set will automatically have a response sent to them containing the message handler's result.
    3. rpc: Perform an RPC by publishing a message with its reply_to parameter set to the relevant value.

    To perform these operations, each adapter instance receives a RabbitMQConnectionData instance, which is used to create a AsyncConnection instance. This is in turn used to create the Publisher and Consumer instances necessary to work with the queues and exchanges specified in the configurations given to the AsyncAdapter in its constructor. Each instance of the class also maintains a dictionary of correlation IDs relevant to messages we are waiting on a response for, namely RPC requests.

Todo

  • Implement Prometheus metrics support.
  • Server example - refactor it to render real HTML

Notes

This package is inspired by various implementations that I have encountered over the years. The current version includes improvements and adjustments designed to improve integration with technologies and frameworks developed over the last few years:

  • Python 3.9
  • aio-pika 6.8.0
  • RabbitMQ Server 3.8.3 on Ubuntu 18
You might also like...
A TCP Chatroom built with python and TCP/IP sockets, consisting of a server and multiple clients which can connect with the server and chat with each other.

A TCP Chatroom built with python and TCP/IP sockets, consisting of a server and multiple clients which can connect with the server and chat with each other. It also provides an Admin role with features including kicking and baning of users.

9SPY: a Windows RAT built in Python using sockets

9SPY 👁‍🗨 9SPY is a Windows RAT built in Python using sockets Features Features will be listed here soon, there are currenly 14 Information This is a

Serves some data over HTTP, once. Based on the built-in Python module http.server

serve-me-once Serves some data over HTTP, once. Based on the built-in Python module http.server.

Best discord webhook spammer using proxy (support all proxy type)
Best discord webhook spammer using proxy (support all proxy type)

Best discord webhook spammer using proxy (support all proxy type)

Implementing Cisco Support APIs into NetBox
Implementing Cisco Support APIs into NetBox

NetBox Cisco Support API Plugin NetBox plugin using Cisco Support APIs to gather EoX and Contract coverage information for Cisco devices. Compatibilit

It's an extra broadcast driver for masonite. It adds support for socketio.

It's an extra broadcast driver for masonite. It adds support for socketio.

Pritunl is a distributed enterprise vpn server built using the OpenVPN protocol.
Pritunl is a distributed enterprise vpn server built using the OpenVPN protocol.

Pritunl is a distributed enterprise vpn server built using the OpenVPN protocol.

A Python library to ease the integration with the Beem Africa (SMS, AIRTIME, OTP, 2WAY-SMS, BPAY, USSD)

python-client A Python library to easy the integration with the Beem Africa SMS Gateway Features to be Implemented Airtime OTP SMS Two way SMS USSD Bp

A Python library to utilize AWS API Gateway's large IP pool as a proxy to generate pseudo-infinite IPs for web scraping and brute forcing.

A Python library to utilize AWS API Gateway's large IP pool as a proxy to generate pseudo-infinite IPs for web scraping and brute forcing.

Comments
  • BunnyStorm features

    BunnyStorm features

    Add ability to stop AsyncAdapter gracefully, expose adapter consumers and producers as properties, make consumers and producers kinder with unexpected kwargs, fix message acking using message context manager, and allow using a custom routing key for adapter RPC

    opened by johnsmith400 0
  • Improved some parts of publisher and consumer

    Improved some parts of publisher and consumer

    • Made the publish message log limited to 100 chars.
    • added the option to publish to the default exchange using AsyncAdapter.publish.
    • Added exclusive parameter to the receive and consume methods in order to control the exclusivity of the consumption when not using rpc directly
    opened by josh-gamba 0
  • Please support ssl connection string

    Please support ssl connection string

    Support ssl connection (needed for AWS RabbitMQ service) changes needed only in rabbitmq_connection_data.py:

    --- a/bunny_storm/rabbitmq_connection_data.py
    +++ b/bunny_storm/rabbitmq_connection_data.py
    @@ -14,6 +14,7 @@ class RabbitMQConnectionData:
         port: int = 5672
         virtual_host: str = "/"
         connection_name: str = ""
    +    ssl: bool = False
    
         def uri(self) -> str:
             """
    @@ -22,4 +23,5 @@ class RabbitMQConnectionData:
             """
             vhost = "" if self.virtual_host == "/" else self.virtual_host
             name_query = f"?name={self.connection_name}" if self.connection_name else ""
    -        return f"amqp://{self.username}:{self.password}@{self.host}:{self.port}/{vhost}{name_query}"
    +        header = "amqps" if self.ssl else "amqp"
    +        return f"{header}://{self.username}:{self.password}@{self.host}:{self.port}/{vhost}{name_query}"
    
    

    Didn't had permissions to post PR myself.

    Thanks, Tomer

    opened by TomerGershi 1
Owner
null
Light, simple RPC framework for Python

Agileutil是一个Python3 RPC框架。基于微服务架构,封装了rpc/http/orm/log等常用组件,提供了简洁的API,开发者可以很快上手,快速进行业务开发。

null 16 Nov 22, 2022
Discord RPC Generator With Python

Discord-RPC-Generator Thank you for using this Discord Custom RP Generator. This is 100% safe and open source. Download Discord for your computer here

null 1 Nov 9, 2021
Out-of-box Python RPC framework

typed-jsonrpc Out-of-box Python RPC framework. WIP. Make LSP easy for everyone. The conception of final usage: from typed_jsonrpc import * ls = Langu

Taine Zhao 4 Dec 28, 2021
Process incoming JSON-RPC requests in Python

August 16, 2021: Version 5 has been released. Read about the changes in version 5, or read the full documentation. Version 5 is for Python 3.8+ only.

Exploding Labs 156 Dec 31, 2022
Qobuz-rpc - A simple discord rich presence client for qobuz written in Python

qobuz-rpc A simple discord rich presence client for qobuz written in Python It's

Raphael O. 13 Dec 15, 2022
This tool extracts Credit card numbers, NTLM(DCE-RPC, HTTP, SQL, LDAP, etc), Kerberos (AS-REQ Pre-Auth etype 23), HTTP Basic, SNMP, POP, SMTP, FTP, IMAP, etc from a pcap file or from a live interface.

This tool extracts Credit card numbers, NTLM(DCE-RPC, HTTP, SQL, LDAP, etc), Kerberos (AS-REQ Pre-Auth etype 23), HTTP Basic, SNMP, POP, SMTP, FTP, IMAP, etc from a pcap file or from a live interface.

null 1.6k Jan 1, 2023
A simple implementation of an RPC toolkit

Simple RPC With Raw Sockets Repository for the Data network course project: Introduction In this project, you will attempt to code a simple implementa

Milad Samimifar 1 Mar 25, 2022
Asynchronous For Python(asyncio)

asyncio is a library to write concurrent code using the async/await syntax.asyncio is used as a foundation for multiple Python asynchronous frameworks that provide high-performance network and web-servers, database connection libraries, distributed task queues, etc. asyncio is often a perfect fit for IO-bound and high-level structured network code.

Janak raikhola 0 Feb 5, 2022
The can package provides controller area network support for Python developers

python-can The Controller Area Network is a bus standard designed to allow microcontrollers and devices to communicate with each other. It has priorit

Brian Thorne 904 Dec 29, 2022
Build custom OSINT tools and APIs (Ping, Traceroute, Scans, Archives, DNS, Scrape, Whois, Metadata & built-in database for more info) with this python package

Build custom OSINT tools and APIs with this python package - It includes different OSINT modules (Ping, Traceroute, Scans, Archives, DNS, Scrape, Whoi

QeeqBox 52 Jan 6, 2023