diff --git a/sylvilagus/python/sylvilagus/ch04/alert_consumer.py b/sylvilagus/python/sylvilagus/ch04/alert_consumer.py index 00900e0..d493248 100644 --- a/sylvilagus/python/sylvilagus/ch04/alert_consumer.py +++ b/sylvilagus/python/sylvilagus/ch04/alert_consumer.py @@ -12,8 +12,8 @@ AMQP_USER = 'alert_user' AMQP_PASS = 'alertme' AMQP_VHOST = '/' AMQP_EXCHANGE = 'alerts' -OPS_EMAILS = ['daniel.buch+sylvilagus-alerts-ops@gmail.com'] -ADMIN_EMAILS = ['daniel.buch+sylvilagus-alerts-admin@gmail.com'] +OPS_EMAILS = ['me@localhost'] +ADMIN_EMAILS = ['me@localhost'] def main(): @@ -61,7 +61,7 @@ def send_mail(recipients, subject, message): 'To: ', 'Date: ', 'Subject: {}'.format(subject) - ]) + '\r\n' + ]) + '\r\n\r\n' smtp_server = smtplib.SMTP() smtp_server.connect('localhost', 25) smtp_server.sendmail('alerts@sylvilagus.local', diff --git a/sylvilagus/python/sylvilagus/ch04/alert_producer.py b/sylvilagus/python/sylvilagus/ch04/alert_producer.py new file mode 100644 index 0000000..d579f72 --- /dev/null +++ b/sylvilagus/python/sylvilagus/ch04/alert_producer.py @@ -0,0 +1,51 @@ +from __future__ import print_function + +import json +import sys + +import pika + +from argparse import ArgumentParser + + +def main(sysargs=sys.argv[:]): + parser = ArgumentParser() + parser.add_argument('-r', + '--routing-key', + help='Routing key for message (e.g. myalert.im)') + parser.add_argument('-m', + '--message', + help='Message text for alert.') + + args = parser.parse_args(sysargs[1:]) + + creds_broker = pika.PlainCredentials('alert_user', 'alertme') + conn_params = pika.ConnectionParameters('localhost', + virtual_host='/', + credentials=creds_broker) + conn_broker = pika.BlockingConnection(conn_params) + + channel = conn_broker.channel() + + msg = json.dumps({'message': args.message}) + msg_props = pika.BasicProperties() + msg_props.content_type = 'application/json' + msg_props.durable = False + + channel.basic_publish(body=msg, + exchange='alerts', + properties=msg_props, + routing_key=args.routing_key) + + print( + ('Sent message {} tagged with routing key {!r} to ' + + 'exchange "alerts" on vhost "/".').format(msg, args.routing_key) + ) + + conn_broker.close() + + return 0 + + +if __name__ == '__main__': + sys.exit(main())