Python Client API

powerful. fast. easy.

The AMPS Client for Python combines performance, flexibility and ease-of-use in just the right way to make your messaging applications shine. Combine Python's productivity and expresiveness with the blazing performance of AMPS to create applications that win. The Python client is built on our C++ client, for the highest levels of performance and capability. Here are a few examples to get you started:

Example 1: Connect and Subscribe

 1 from AMPS import *
 3 amps = Client("my-application")
 5 try:
 6   amps.connect("tcp://localhost:9007/amps/json")
 7   amps.logon()
 9   for message in amps.subscribe("orders"):
10      print message.get_data()
12 except AMPSException, e:
13    print e

In this example, we connect to an AMPS server running locally and initiate a subscription to the "orders" topic. As new orders are posted, myListener is invoked with each message, and prints the data of each order to the console.

Example 2: Publish a Message

1 amps = Client("publish-example")
2 amps.connect("tcp://")
3 amps.logon()
4 amps.publish("messages", "<hi>Hello, world!</hi>")

With AMPS, publishing is simple, as shown in this example. We connect to an AMPS server running locally, and publish a single message to the messages topic. To simply publish a message, there is no need to predeclare the topic or configure complex routing. Any subscription that has asked for XML messages on the messages topic will receive the message.

Example 3: Automatic Reconnection and Resubscription

Rock-solid applications must be able to recover from network outages. The AMPS Python client includes an HAClient class that includes automatic reconnection and resubscription. Best of all, easy-to-implement interfaces control reconnection and resubscription behavior, allowing you to easily customize failover.

 1 client = AMPS.HAClient("reconnectingSubscriber")
 3 # The ServerChooser interface tells the HAClient which server
 4 # to connect to, both for the initial connection and failover.
 5 # The DefaultServerChooser is included with the client: many
 6 # applications implement a ServerChooser to control failover
 7 # behavior.
 9 sc = AMPS.DefaultServerChooser()
10 sc.add("tcp://amps-server:9007/amps/json")
11 sc.add("tcp://amps-failover-server:9007/amps/json")
13 client.set_server_chooser(sc)
14 client.connect_and_logon()
16 # Enter subscriptions. If the connection to the server
17 # is lost, the HAClient will restore the connection to
18 # the server or the failover partner, and restore
19 # subscriptions.
21 client.subscribe(handleMessage, "my-cool-topic")

The HAClient class can, optionally, also provide store-and-forward for reliable publish. With the AMPS transaction log, the class can provide resumable subscriptions that are guaranteed not to miss messages or receive duplicate messages, even in the case of failover between replicated servers.

Example 4: Query the Contents of a "SOW" Topic

State-of-the-World ("SOW") topics in AMPS combine the power of a database table with the performance of a publish-subscribe system. Use the AMPS Python client to query the contents of a SOW topic.

 1 def execute_sow_query(client):
 2     # runs the SOW query, processes all of the messages
 3     # from the query, and then returns.
 4     for message in client.sow("orders",
 5                               "/symbol='ROL'",
 6                               batch_size=100,
 7                               timeout=5000):
 9         if message.get_command() == Message.Command.SOW:
10            print message.get_data()

This example queries for all orders for the symbol ROL, and simply prints the messages to the console.

Example 5: Automatic Failover

The AMPS Python client includes both a basic client, and a high availability client with additional features, including the ability to automatically failover if the client is disconnected. This example shows how to set up a high availability client for failover:

 1 client = AMPS.HAClient("myClient")
 3 # is the primary AMPS instance, and
 4 # is the secondary
 6 # create a server chooser
 7 chooser = AMPS.DefaultServerChooser()
 9 # add the addresses to use for failover
10 chooser.add("tcp://")
11 chooser.add("tcp://")
13 # set the server chooser for the client
14 client.set_server_chooser(chooser)
16 # connect and logon
17 client.connect_and_logon()
19 # now, use the client: if the client detects
20 # a disconnection, it will reconnect.
22 ...
24 # at the end of the program, disconnect
26 client.disconnect()

This example creates an HA client and a server chooser for the client. The code then populates the server chooser with the list of failover servers, adds the chooser to the client, and then connects.

Once the client is connected, you can use the HAClient object just like a regular AMPS client. You can take advantage of the extended features, such as durable publish, duplicate message protection, and so on -- see the Developer's Guide for more information!

Everything You Need

Ready to learn more? The AMPS Client for Python includes everything you need to get started: libraries, sample applications, developer's guide, and reference documentation. Each AMPS Client includes complete source code so you can fully learn and understand AMPS, and best integrate it into your environment.

Click here to download and start using the AMPS Client for Python. Once you've downloaded it, read the Developer Guide to get started.

If you need more help getting started, the 60East Technologies support and services team is ready to provide the support you need to help you CRANK UP THE AMPS.

The current release of the Python client is version, which supports AMPS servers version 5.3 and earlier.