Python client and examples



Python client is updated and also available on pip for your convenience! It also comes with a neat PUB-SUB example that uses our publicly accessible server, public.nextuser.com:7777.

There is documentation for python client available here. We’ll also go over the code here for a quick reference.

First, get the client:

$ pip install pubsubsql

To set up a subscriber running in a matter of seconds, just connect, subscribe and wait for data to arrive:

import time
import random

import pubsubsql as pss


subscriber = pss.Client()

subscriber.connect("public.pubsubsql.com:7777")

subscriber.execute("subscribe * from Stocks where MarketCap = 'MEGA CAP'")

timeout = 60000

while subscriber.waitForPubSub(timeout):
    print "*********************************"
    print "Action:{}".format(subscriber.getAction())
    while subscriber.nextRow():
        for ordinal, column in enumerate(subscriber.getColumns()):
            print "{}:{}".format(column, subscriber.getValue(ordinal)),
        print ""

And that’s it! Just build it and run it!

Running the subscriber on public server

In case you don’t see any data, no one is publishing it. In that case, you need to build the publisher as well. This publisher will randomly update the price of Google stock:

import time
import random

import pubsubsql as pss


client = pss.Client()

client.connect("public.pubsubsql.com:7777")

try:
    client.execute("key Stocks Ticker")
    client.execute("tag Stocks MarketCap")
except:
    # key or tag may have already been defined, so its ok
    pass

try:
    client.execute("insert into Stocks (Ticker, Price, MarketCap) values (GOOG, '1,2002d.22', 'MEGA CAP')")
except:
    pass

while 1:
    time.sleep(0.3)
    command = "update Stocks set Price='%f' where Ticker='GOOG'" % (10000.0 * random.uniform(0.9, 1.9))
    client.execute(command)

If you have any issues, just log an issue to github project or send us an email!






Share this post


comments powered by Disqus