diff options
| author | 2018-05-22 22:12:43 +0200 | |
|---|---|---|
| committer | 2018-05-22 22:12:43 +0200 | |
| commit | f689ce549689b3192ce1f7510804495acfd7869d (patch) | |
| tree | 56ac7af58d1b9a8ea59a943e711e337a10220cdd /webapp/rmq.py | |
| parent | Delete test.py (diff) | |
adds webapp and docker-compose for more proof of concept
Diffstat (limited to '')
| -rw-r--r-- | webapp/rmq.py | 48 | 
1 files changed, 48 insertions, 0 deletions
diff --git a/webapp/rmq.py b/webapp/rmq.py new file mode 100644 index 0000000..80d418a --- /dev/null +++ b/webapp/rmq.py @@ -0,0 +1,48 @@ +import os +import pika + +USERNAME      = 'guest' +PASSWORD      = 'guest' +HOST          = os.environ.get('RMQ_HOST', '172.17.0.2') +PORT          = 5672 +EXCHANGE      = 'exchange' +EXCHANGE_TYPE = 'direct' +QUEUE         = 'text' +ROUTING_KEY   = 'bacon' + +try: +    import docker +    client = docker.from_env() +    containers = client.containers.get('snekbox_pdrmq_1') +    print("Attempting to get rabbitmq host automatically") +    HOST = list(containers.attrs.get('NetworkSettings').get('Networks').values())[0]['IPAddress'] +    print(f"found {HOST}") +except: +    pass + +def send(message): +    credentials = pika.PlainCredentials(USERNAME, PASSWORD) +    connection = pika.BlockingConnection(pika.ConnectionParameters(HOST, PORT, '/', credentials)) +    properties = pika.BasicProperties(content_type='text/plain', delivery_mode=1) + +    channel = connection.channel() +    channel.queue_declare(queue=QUEUE, durable=False) +    channel.exchange_declare(exchange=EXCHANGE, exchange_type=EXCHANGE_TYPE) +    channel.queue_bind(exchange=EXCHANGE, queue=QUEUE, routing_key=ROUTING_KEY) + +    result = channel.basic_publish( +                exchange=EXCHANGE, +                routing_key=ROUTING_KEY, +                body=message, +                properties=properties +    ) + +    if result: +        print(f"""Connecting to\nhost:     {HOST}\nport:     {PORT}\nexchange: {EXCHANGE}\nqueue:    {QUEUE}""", flush=True) +        print(f"Sent: '{message}'") +    else: +        print("not delivered") + +    connection.close() + +#send('print("bacon is delicious")')  |