Created
June 28, 2019 07:56
-
-
Save tjovicic/4e28001490c5944d79d6144353a3c34e to your computer and use it in GitHub Desktop.
Simple RabbitMQ HTTP publisher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import httplib | |
import base64 | |
def main(): | |
parser = argparse.ArgumentParser(description='RabbitMQ publisher') | |
parser.add_argument('url', help='AMQP host') | |
parser.add_argument('username', help='Client username') | |
parser.add_argument('password', help='Client password') | |
parser.add_argument('--exchange', dest='exchange', required=True, help='Exchange') | |
parser.add_argument('--routing-key', dest='routing_key', required=True, help='Routing key') | |
parser.add_argument('--body', dest='body', help='Message body') | |
args = parser.parse_args() | |
conn = httplib.HTTPConnection(args.url) | |
body = "{'properties': {'delivery_mode': 1}, 'routing_key': '%s', 'payload': '%s', 'payload_encoding': 'string'}" % (args.routing_key, args.body) | |
headers = { | |
'Authorization': 'Basic %s' % base64.encodestring('%s:%s' % (args.username, args.password)).replace('\n', ''), | |
'Content-type': 'application/json'} | |
conn.request('POST', '/api/exchanges/%%2f/%s/publish' % args.exchange, body=body, headers=headers) | |
response = conn.getresponse() | |
print response.status, response.read().decode('utf-8') | |
conn.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment