This client program connects to a server using <connection-url>, creates a sender for target <address>, sends a message containing <message-body>, closes the connection, and exits.
Example: Sending messages
from __future__ import print_function
import sys
from proton import Message
from proton.handlers import MessagingHandler
from proton.reactor import Container
class SendHandler(MessagingHandler):
def __init__(self, conn_url, address, message_body):
super(SendHandler, self).__init__()
self.conn_url = conn_url
self.address = address
self.message_body = message_body
def on_start(self, event):
conn = event.container.connect(self.conn_url)
# To connect with a user and password:
# conn = event.container.connect(self.conn_url, user="<user>", password="<password>")
event.container.create_sender(conn, self.address)
def on_link_opened(self, event):
print("SEND: Opened sender for target address '{0}'".format
(event.sender.target.address))
def on_sendable(self, event):
message = Message(self.message_body)
event.sender.send(message)
print("SEND: Sent message '{0}'".format(message.body))
event.sender.close()
event.connection.close()
def main():
try:
conn_url, address, message_body = sys.argv[1:4]
except ValueError:
sys.exit("Usage: send.py <connection-url> <address> <message-body>")
handler = SendHandler(conn_url, address, message_body)
container = Container(handler)
container.run()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
pass
from __future__ import print_function
import sys
from proton import Message
from proton.handlers import MessagingHandler
from proton.reactor import Container
class SendHandler(MessagingHandler):
def __init__(self, conn_url, address, message_body):
super(SendHandler, self).__init__()
self.conn_url = conn_url
self.address = address
self.message_body = message_body
def on_start(self, event):
conn = event.container.connect(self.conn_url)
# To connect with a user and password:
# conn = event.container.connect(self.conn_url, user="<user>", password="<password>")
event.container.create_sender(conn, self.address)
def on_link_opened(self, event):
print("SEND: Opened sender for target address '{0}'".format
(event.sender.target.address))
def on_sendable(self, event):
message = Message(self.message_body)
event.sender.send(message)
print("SEND: Sent message '{0}'".format(message.body))
event.sender.close()
event.connection.close()
def main():
try:
conn_url, address, message_body = sys.argv[1:4]
except ValueError:
sys.exit("Usage: send.py <connection-url> <address> <message-body>")
handler = SendHandler(conn_url, address, message_body)
container = Container(handler)
container.run()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
pass
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Running the example
To run the example program, copy it to a local file and invoke it using the python command.
python send.py amqp://localhost queue1 hello
$ python send.py amqp://localhost queue1 hello
Copy to ClipboardCopied!Toggle word wrapToggle overflow
This client program connects to a server using <connection-url>, creates a receiver for source <address>, and receives messages until it is terminated or it reaches <count> messages.
Example: Receiving messages
from __future__ import print_function
import sys
from proton.handlers import MessagingHandler
from proton.reactor import Container
class ReceiveHandler(MessagingHandler):
def __init__(self, conn_url, address, desired):
super(ReceiveHandler, self).__init__()
self.conn_url = conn_url
self.address = address
self.desired = desired
self.received = 0
def on_start(self, event):
conn = event.container.connect(self.conn_url)
# To connect with a user and password:
# conn = event.container.connect(self.conn_url, user="<user>", password="<password>")
event.container.create_receiver(conn, self.address)
def on_link_opened(self, event):
print("RECEIVE: Created receiver for source address '{0}'".format
(self.address))
def on_message(self, event):
message = event.message
print("RECEIVE: Received message '{0}'".format(message.body))
self.received += 1
if self.received == self.desired:
event.receiver.close()
event.connection.close()
def main():
try:
conn_url, address = sys.argv[1:3]
except ValueError:
sys.exit("Usage: receive.py <connection-url> <address> [<message-count>]")
try:
desired = int(sys.argv[3])
except (IndexError, ValueError):
desired = 0
handler = ReceiveHandler(conn_url, address, desired)
container = Container(handler)
container.run()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
pass
from __future__ import print_function
import sys
from proton.handlers import MessagingHandler
from proton.reactor import Container
class ReceiveHandler(MessagingHandler):
def __init__(self, conn_url, address, desired):
super(ReceiveHandler, self).__init__()
self.conn_url = conn_url
self.address = address
self.desired = desired
self.received = 0
def on_start(self, event):
conn = event.container.connect(self.conn_url)
# To connect with a user and password:
# conn = event.container.connect(self.conn_url, user="<user>", password="<password>")
event.container.create_receiver(conn, self.address)
def on_link_opened(self, event):
print("RECEIVE: Created receiver for source address '{0}'".format
(self.address))
def on_message(self, event):
message = event.message
print("RECEIVE: Received message '{0}'".format(message.body))
self.received += 1
if self.received == self.desired:
event.receiver.close()
event.connection.close()
def main():
try:
conn_url, address = sys.argv[1:3]
except ValueError:
sys.exit("Usage: receive.py <connection-url> <address> [<message-count>]")
try:
desired = int(sys.argv[3])
except (IndexError, ValueError):
desired = 0
handler = ReceiveHandler(conn_url, address, desired)
container = Container(handler)
container.run()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
pass
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Running the example
To run the example program, copy it to a local file and invoke it using the python command.
python receive.py amqp://localhost queue1
$ python receive.py amqp://localhost queue1
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Wir helfen Red Hat Benutzern, mit unseren Produkten und Diensten innovativ zu sein und ihre Ziele zu erreichen – mit Inhalten, denen sie vertrauen können. Entdecken Sie unsere neuesten Updates.
Mehr Inklusion in Open Source
Red Hat hat sich verpflichtet, problematische Sprache in unserem Code, unserer Dokumentation und unseren Web-Eigenschaften zu ersetzen. Weitere Einzelheiten finden Sie in Red Hat Blog.
Über Red Hat
Wir liefern gehärtete Lösungen, die es Unternehmen leichter machen, plattform- und umgebungsübergreifend zu arbeiten, vom zentralen Rechenzentrum bis zum Netzwerkrand.