summaryrefslogtreecommitdiff
path: root/demo/demo_send.py
blob: 27bb1b107f7cb2b66a8d6e3e08dcb9a2d97f4c7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
"""
Test AMQP library.

Send a message to the corresponding demo_receive.py script, any
arguments to this program are joined together and sent as a message
body.

2007-11-11 Barry Pederson <bp@barryp.org>

"""
import sys
from optparse import OptionParser

import amqp


def main():
    parser = OptionParser(
        usage='usage: %prog [options] message\nexample: %prog hello world',
    )
    parser.add_option(
        '--host', dest='host',
        help='AMQP server to connect to (default: %default)',
        default='localhost',
    )
    parser.add_option(
        '-u', '--userid', dest='userid',
        help='userid to authenticate as (default: %default)',
        default='guest',
    )
    parser.add_option(
        '-p', '--password', dest='password',
        help='password to authenticate with (default: %default)',
        default='guest',
    )
    parser.add_option(
        '--ssl', dest='ssl', action='store_true',
        help='Enable SSL (default: not enabled)',
        default=False,
    )

    options, args = parser.parse_args()

    if not args:
        parser.print_help()
        sys.exit(1)

    msg_body = ' '.join(args)

    conn = amqp.Connection(options.host, userid=options.userid,
                           password=options.password, ssl=options.ssl)

    ch = conn.channel()
    ch.exchange_declare('myfan', 'fanout')

    msg = amqp.Message(msg_body, content_type='text/plain',
                       application_headers={'foo': 7, 'bar': 'baz'})

    ch.basic_publish(msg, 'myfan')

    ch.close()
    conn.close()

if __name__ == '__main__':
    main()