-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.py
52 lines (40 loc) · 1.58 KB
/
test.py
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
#!/usr/bin/env python
import pika
import sys
from optparse import OptionParser
from taskqueue.confparser import ConfigParser, NoSectionError
def parse_cmdline(defaults):
"""Parse commandline options."""
parser = OptionParser()
parser.add_option("-c", "--config", dest="config",
default="/etc/taskqueue/config.ini",
help="path to config file")
parser.add_option("-t", "--content-type", dest="ctype",
default="text/plain",
help="content type of AMQP message.")
return parser.parse_args()
options, args = parse_cmdline({})
config = ConfigParser()
config.read(options.config)
amqp_items = dict(config.items("amqp"))
amqp_host = amqp_items.get("host", "localhost")
amqp_user = amqp_items.get("user", "guest")
amqp_passwd = amqp_items.get("passwd", "guest")
amqp_vhost = amqp_items.get("vhost", "/")
credentials = pika.PlainCredentials(amqp_user, amqp_passwd)
connection = pika.BlockingConnection(pika.ConnectionParameters(
credentials=credentials,
host=amqp_host,
virtual_host=amqp_vhost))
channel = connection.channel()
channel.queue_declare(queue='taskqueue', durable=True)
message = ' '.join(args) or "Hello World!"
channel.basic_publish(exchange='',
routing_key='taskqueue',
body=message,
properties=pika.BasicProperties(
delivery_mode=2, # make message persistent
content_type=options.ctype
))
print " [x] Sent %r" % (message,)
connection.close()