Skip to content

Instantly share code, notes, and snippets.

@tjovicic
Created June 28, 2019 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjovicic/4e28001490c5944d79d6144353a3c34e to your computer and use it in GitHub Desktop.
Save tjovicic/4e28001490c5944d79d6144353a3c34e to your computer and use it in GitHub Desktop.
Simple RabbitMQ HTTP publisher
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