55 lines
2.2 KiB
Python
55 lines
2.2 KiB
Python
|
#!/usr/bin/python3
|
||
|
|
||
|
# MIT License
|
||
|
|
||
|
# Copyright (c) 2024 Thomas Williams - https://git.server.wales/thomas
|
||
|
|
||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
|
# of this software and associated documentation files (the "Software"), to deal
|
||
|
# in the Software without restriction, including without limitation the rights
|
||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
|
# copies of the Software, and to permit persons to whom the Software is
|
||
|
# furnished to do so, subject to the following conditions:
|
||
|
|
||
|
# The above copyright notice and this permission notice shall be included in all
|
||
|
# copies or substantial portions of the Software.
|
||
|
|
||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||
|
# SOFTWARE.
|
||
|
|
||
|
import pika
|
||
|
import ssl
|
||
|
|
||
|
class rabbitMQClient:
|
||
|
|
||
|
def __init__(self, ca, cacert, cakey, rabbitmqHost, rabbitmqPort, routingKey):
|
||
|
|
||
|
self.ca = ca
|
||
|
self.cacert = cacert
|
||
|
self.cakey = cakey
|
||
|
self.rabbitmqHost = rabbitmqHost
|
||
|
self.rabbitmqPort = rabbitmqPort
|
||
|
self.routingKey = routingKey
|
||
|
|
||
|
def publish(self, message):
|
||
|
|
||
|
context = ssl.create_default_context(
|
||
|
cafile=self.ca)
|
||
|
context.verify_mode = ssl.CERT_REQUIRED
|
||
|
context.load_cert_chain(self.cacert, self.cakey)
|
||
|
|
||
|
sslOptions = pika.SSLOptions(context, self.rabbitmqHost)
|
||
|
|
||
|
connParams = pika.ConnectionParameters(host=self.rabbitmqHost,port=self.rabbitmqPort, ssl_options=sslOptions, credentials=pika.credentials.ExternalCredentials())
|
||
|
|
||
|
with pika.BlockingConnection(connParams) as conn:
|
||
|
|
||
|
ch = conn.channel()
|
||
|
|
||
|
ch.basic_publish(exchange='', routing_key=self.routingKey, body=message, properties=pika.BasicProperties(delivery_mode=pika.spec.PERSISTENT_DELIVERY_MODE))
|