summaryrefslogtreecommitdiff
path: root/python/examples/api
diff options
context:
space:
mode:
authorRafael H. Schloming <rhs@apache.org>2009-10-11 20:39:01 +0000
committerRafael H. Schloming <rhs@apache.org>2009-10-11 20:39:01 +0000
commita3e5e307fbebe7791161bc080a37d67d9b5cb631 (patch)
tree2df21813483ba6585ce43386a4d19d2e01126d9c /python/examples/api
parentfbc01ca9a7908de4a8b3e75930b9cb1662ddb741 (diff)
downloadqpid-python-a3e5e307fbebe7791161bc080a37d67d9b5cb631.tar.gz
added ability to specify reply-to and message properties for ping; also added server example
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@824145 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'python/examples/api')
-rwxr-xr-xpython/examples/api/ping41
-rwxr-xr-xpython/examples/api/server80
2 files changed, 114 insertions, 7 deletions
diff --git a/python/examples/api/ping b/python/examples/api/ping
index 59b367cca6..7d3686e7cf 100755
--- a/python/examples/api/ping
+++ b/python/examples/api/ping
@@ -22,6 +22,16 @@ import optparse, time
from qpid.messaging import *
from qpid.util import URL
+def nameval(st):
+ idx = st.find("=")
+ if idx >= 0:
+ name = st[0:idx]
+ value = st[idx+1:]
+ else:
+ name = st
+ value = None
+ return name, value
+
parser = optparse.OptionParser(usage="usage: %prog [options] ADDRESS [ CONTENT ... ]",
description="Drain messages from the supplied address.")
parser.add_option("-b", "--broker", default="localhost",
@@ -30,9 +40,12 @@ parser.add_option("-c", "--count", type=int, default=1,
help="stop after count messages have been sent, zero disables (default %default)")
parser.add_option("-t", "--timeout", type=float, default=None,
help="exit after the specified time")
-parser.add_option("-m", "--map", action="store_true",
- help="interpret content as map")
parser.add_option("-i", "--id", help="use the supplied id instead of generating one")
+parser.add_option("-r", "--reply-to", help="specify reply-to address")
+parser.add_option("-P", "--property", dest="properties", action="append", default=[],
+ help="specify message property")
+parser.add_option("-M", "--map", dest="entries", action="append", default=[],
+ help="specify map entry for message body")
opts, args = parser.parse_args()
@@ -45,12 +58,23 @@ if args:
addr = args.pop(0)
else:
parser.error("address is required")
+
+content = None
+
if args:
- content = " ".join(args)
- if opts.map:
- content = eval(content)
+ text = " ".join(args)
+else:
+ text = None
+
+if opts.entries:
+ content = {}
+ if text:
+ content["text"] = text
+ for e in opts.entries:
+ name, val = nameval(e)
+ content[name] = val
else:
- content = None
+ content = text
# XXX: should make URL default the port for us
conn = Connection.open(url.host, url.port or AMQP_PORT,
@@ -62,8 +86,11 @@ count = 0
start = time.time()
while (opts.count == 0 or count < opts.count) and \
(opts.timeout is None or time.time() - start < opts.timeout):
- msg = Message(content)
+ msg = Message(content, reply_to=opts.reply_to)
msg.properties["ping-id"] = "%s:%s" % (ping_id, count)
+ for p in opts.properties:
+ name, val = nameval(p)
+ msg.properties[name] = val
try:
snd.send(msg)
diff --git a/python/examples/api/server b/python/examples/api/server
new file mode 100755
index 0000000000..4e986f5ac8
--- /dev/null
+++ b/python/examples/api/server
@@ -0,0 +1,80 @@
+#!/usr/bin/env python
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+import optparse, sys, traceback
+from qpid.messaging import *
+from qpid.util import URL
+from subprocess import Popen, STDOUT, PIPE
+
+parser = optparse.OptionParser(usage="usage: %prog [options] ADDRESS ...",
+ description="handle requests from the supplied address.")
+parser.add_option("-b", "--broker", default="localhost",
+ help="connect to specified BROKER (default %default)")
+
+opts, args = parser.parse_args()
+
+url = URL(opts.broker)
+if args:
+ addr = args.pop(0)
+else:
+ parser.error("address is required")
+
+# XXX: should make URL default the port for us
+conn = Connection.open(url.host, url.port or AMQP_PORT,
+ username=url.user, password=url.password)
+conn.reconnect = True
+ssn = conn.session()
+rcv = ssn.receiver(addr)
+
+def dispatch(msg):
+ msg_type = msg.properties.get("type")
+ if msg_type == "shell":
+ proc = Popen(msg.content, shell=True, stderr=STDOUT, stdin=PIPE, stdout=PIPE)
+ output, _ = proc.communicate()
+ result = Message(output)
+ result.properties["exit"] = proc.returncode
+ elif msg_type == "eval":
+ try:
+ content = eval(msg.content)
+ except:
+ content = traceback.format_exc()
+ result = Message(content)
+ else:
+ result = Message("unrecognized message type: %s" % msg_type)
+ return result
+
+while True:
+ try:
+ msg = rcv.fetch()
+ response = dispatch(msg)
+ snd = ssn.sender(msg.reply_to)
+ try:
+ snd.send(response)
+ except SendError, e:
+ print e
+ snd.close()
+ ssn.acknowledge()
+ except Empty:
+ break
+ except ReceiveError, e:
+ print e
+ break
+
+conn.close()