本文整理汇总了Python中Pubnub.Pubnub类的典型用法代码示例。如果您正苦于以下问题:Python Pubnub类的具体用法?Python Pubnub怎么用?Python Pubnub使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Pubnub类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: push
def push(cls, channel, message_dict):
pubnub = Pubnub(PUBLISH_KEY, SUBSCRIBE_KEY, SECRET_KEY, False )
info = pubnub.publish({
'channel' : channel,
'message' : json.dumps(message_dict)
})
return info
示例2: RemotePubNub
class RemotePubNub():
def __init__(self, PUBNUB_PUBLISH_KEY="demo", PUBNUB_SUBSCRIBE_KEY="demo", queue=None):
self.pubnub = Pubnub(publish_key=PUBNUB_PUBLISH_KEY, subscribe_key=PUBNUB_SUBSCRIBE_KEY)
self.queue = queue
def get_queue(self):
return self.queue
def publish(self, channel, message):
self.pubnub.publish(channel, message)
#logging.info("message published")
def subscribe(self, channel):
self.pubnub.subscribe(
channel, callback=self.parse_command, error=self.sub_error,
connect=self.sub_connect, disconnect=self.sub_disconnect)
def parse_command(self, message, channel):
if self.queue is not None:
print message
self.queue.put(message)
def sub_error(message):
logging.ERROR(message)
def sub_connect(self, channel):
message = 'Connected to: %s' % channel
logging.info(message)
def sub_disconnect(self, message):
logging.info(message)
示例3: run
def run(self, channel, latency, bufferSize):
self.mutex = Lock()
self.outqueuebuffers = {}
self.outbuffers = {}
self.senders = {}
self.channels = None
self.channel = channel
self.buffer_size = bufferSize
self.timestamp_buffer = [None] * self.buffer_size
self.debug = False
self.index = 0
self.offset = None
self.latency = latency
self.setupDone = False
self.channel_count = 0
pubnub = Pubnub("pub-c-e655613e-f776-4301-9f29-f71edbcd6559",
"sub-c-2eafcf66-c636-11e3-8dcd-02ee2ddab7fe",
"sec-c-ZjUwZDgzMTItYzE2Mi00ZGYyLTg2NGMtNmE5N2Q3MGI0MTli",
False)
self.m = MQTT()
broker = config.mqtt['Broker']
print("Connecting to broker: " + broker)
self.m.connect(broker)
while(True):
pubnub.subscribe({
'channel': self.channel,
'callback': self.setData
})
示例4: post_save
def post_save(cls, sender, document, **kwargs):
cfg = current_app.config
pubnub = Pubnub(
publish_key=cfg["PUBNUB_PUB_KEY"],
subscribe_key=cfg["PUBNUB_SUB_KEY"],
ssl_on=False,
)
pubnub.publish(channel="globaldisaster",
message=json.dumps(document.asdict()))
示例5: main
def main():
resetAVR()
print("Establishing Connections...")
pubnub = Pubnub(publish_key = 'pub-c-f83b8b34-5dbc-4502-ac34-5073f2382d96',
subscribe_key = 'sub-c-34be47b2-f776-11e4-b559-0619f8945a4f',
uuid = "pi")
channel = 'leap2pi'
GPIO.output(4, False)
def _connect(m):
print("Connected to PubNub!")
GPIO.output(4, True)
def _reconnect(m):
print("Reconnected to PubNub!")
GPIO.output(4, True)
def _disconnect(m):
print("Disconnected from PubNub!")
GPIO.output(4, False)
def _callback(m,n):
left_byte = 0
right_byte = 0
# ==============================Left Hand==============================
left_hand = m.get("left_hand",{})
if left_hand != {}:
left_byte = handleLeft(left_hand)
#print(left_hand)
# ==============================Right Hand=============================
right_hand = m.get("right_hand",{})
if right_hand != {}:
right_byte= handleRight(right_hand)
#print(right_hand)
byte = left_byte | right_byte
#print(byte)
#====send i2c=====#
try:
bus.write_byte(0x47,byte)
except IOError:
print("Error!!!!")
resetAVR()
bus.write_byte(0x47,byte)
#Catch and Print Error
def _error(m):
GPIO.output(4, False)
print(m)
#Subscribe to subchannel, set callback function to _callback and set error fucntion to _error
pubnub.subscribe(channels=channel, callback=_callback, error=_error, connect=_connect, reconnect=_reconnect, disconnect=_disconnect)
示例6: startStockPicker
def startStockPicker(server,port):
global globalQueueRef
#Step 1 - Initialize MongoDB & PubNub Connection
mongoconn = pymongo.MongoClient(server,port)
db = mongoconn.stockdb
coll = db.stockcollection
#Setup index on time to fetch the latest update
coll.create_index([('time',DESCENDING)])
#YOUR PUBNUB KEYS - Replace the publish_key and subscriber_key below with your own keys
pubnub = Pubnub(publish_key="<your publish key>",subscribe_key="<your subscribe key>")
#Step 2 - Check and define the metadata ( index names )
metaDataInit(coll)
#Step 3 - Set the parameters , max periodicity , random range
updateTime = 10 #Max ten seconds for every price update
numOfItems = 4 #Four indices to manage
random.seed()
#Step 4 - Setup the Queue and ClientListener Thread
clientQueue = Queue()
clientListener = ClientListenerThread(server,port,clientQueue,pubnub)
clientListener.start()
globalQueueRef = clientQueue
#Step 5 - Setup PubNub Subscription for client requests
pubnub.subscribe("stockhistory", historyCallback,historyError)
#Step 6 - Start the stock picking loop
while True:
#Step 6.1 - Wait for random time
time.sleep(random.randint(1,updateTime))
#Step 6.2 - Wake up and update the stock price for one of the index
newPriceData = getUpdatedPrice(coll)
#Step 6.3 - Update the new price in DB
print "New Price Update " + str(newPriceData)
coll.insert(newPriceData)
#Step 6.4 - Publish over Pubnub , stockdata channel
broadcastData = { 'name' : newPriceData['name'],
'value' : newPriceData['value'],
'change' : newPriceData['change'],
'time' : newPriceData['time'],
}
pubnub.publish('stockdata',json.dumps(broadcastData))
示例7: pubnubmessager
class pubnubmessager(Observer):
def __init__(self):
self.pn = Pubnub(config.pubnub_pubkey, config.pubnub_subkey)
print("Started")
def opportunity(self, profit, volume, buyprice, kask, sellprice, kbid, perc,
weighted_buyprice, weighted_sellprice):
if profit > config.profit_thresh and perc > config.perc_thresh:
message = "profit: %f USD with volume: %f BTC - buy at %.4f (%s) sell at %.4f (%s) ~%.2f%%" % (profit, volume, buyprice, kask, sellprice, kbid, perc)
self.pn.publish({'channel' : config.pubnub_topic, 'message' :{ 'msg' : message}})
示例8: push_cbk
def push_cbk(sender, **kwargs):
istance = kwargs.get('instance')
if istance:
print "Account balance: %s" % istance.balance
pubnub = Pubnub(publish_key='pub-ed4e8fc6-b324-426c-8697-ec763129b026',
subscribe_key='sub-31c9765c-c453-11e1-b76c-1b01c696dab3',
ssl_on=True)
info = pubnub.publish({'channel' : istance.notif_channel,
'message' : 'Current balance ' + unicode(istance.balance) })
print info
示例9: startStockPicker
def startStockPicker(server,port):
global globalQueueRef
global graph
#Step 1 - Initialize MongoDB & PubNub Connection
# py2neo.set_auth_token('%s:%s' % (NEO_HOST, NEO_PORT), NEO_AUTH_TOKEN)
graph = Graph('%s://%s:%[email protected]%s:%s/db/data/' %
(NEO_PROTOCOL, NEO_USER, NEO_PASSWORD, NEO_HOST, NEO_PORT))
#YOUR PUBNUB KEYS - Replace the publish_key and subscriber_key below with your own keys
pubnub = Pubnub(publish_key="<your pub key>",subscribe_key="<your sub key>")
#Step 2 - Check and define the metadata ( index names )
metaDataInit()
#Step 3 - Set the parameters , max periodicity , random range
updateTime = 10 #Max ten seconds for every price update
numOfItems = 4 #Four indices to manage
random.seed()
#Step 4 - Setup the Queue and ClientListener Thread
clientQueue = Queue()
clientListener = ClientListenerThread(server,port,clientQueue,pubnub)
clientListener.start()
globalQueueRef = clientQueue
#Step 5 - Setup PubNub Subscription for client requests
pubnub.subscribe("stockhistory", historyCallback,historyError)
#Step 6 - Start the stock picking loop
while True:
#Step 6.1 - Wait for random time
time.sleep(random.randint(1,updateTime))
#Step 6.2 - Wake up and update the stock price for one of the index
newPriceData = getUpdatedPrice()
#Step 6.3 - Update the new price in DB
print "New Price Update " + str(newPriceData)
#Step 6.4 - Publish over Pubnub , stockdata channel
broadcastData = { 'name' : newPriceData['name'],
'value' : newPriceData['value'],
'change' : newPriceData['change'],
'time' : newPriceData['time'],
}
pubnub.publish('stockdata',json.dumps(broadcastData))
示例10: pinterest
class pinterest(object):
def __init__(self, publish_key,
subscribe_key):
self.publish_key = publish_key
self.subscribe_key = subscribe_key
self.pubnub = Pubnub( 'pub-c-6f928209-b8a8-4131-9749-7470ead38747', 'sub-c-6d212f32-404e-11e4-a498-02ee2ddab7fe', None, False)
def send(self, channel, message):
# Sending message on the channel
self.pubnub.publish({
'channel' : channel,
'message' : message})
示例11: __init__
def __init__(self, publish_key, subscribe_key, uuid):
self.publish_key = publish_key
self.subscribe_key = subscribe_key
self.uuid = uuid
self.pubnub = Pubnub("demo", "demo", None, False)
self.pubnub.uuid = self.uuid
示例12: iotbridge
class iotbridge(object):
def __init__(self, publish_key, subscribe_key, uuid):
self.publish_key = publish_key
self.subscribe_key = subscribe_key
self.uuid = uuid
self.pubnub = Pubnub("demo", "demo", None, False)
self.pubnub.uuid = self.uuid
def send(self, channel, message):
# Sending message on the channel
self.pubnub.publish(channel, message)
def connect(self, channel, callback):
# Listening for messages on the channel
self.pubnub.subscribe(channel, callback=callback)
示例13: init
def init(func):
global pubnub
global pubnub_freeboard
global message_cb
try:
message_cb = func
log.info("pubnub SUBSCRIBE")
pubnub = Pubnub(publish_key=PUB_KEY, subscribe_key=SUB_KEY, cipher_key=CHIPER_KEY, ssl_on=True)
pubnub.subscribe(CHANNEL, callback=pubnub_message_cb, error=pubnub_error_cb, connect=pubnub_connect_cb, reconnect=pubnub_reconnect_cb, disconnect=pubnub_disconnect_cb)
pubnub_freeboard = Pubnub(publish_key=PUB_KEY, subscribe_key=SUB_KEY, ssl_on=False)
log.info("pubnub SUBSCRIBED")
except:
log.error("Error initializing pubnub...Cannot continue")
raise SystemExit
示例14: Streamteam
class Streamteam(object):
def __init__(self, args):
self.channel = args.channel
self.pubnub = Pubnub(
args.publish,
args.subscribe,
None,
False
)
def publish(self, m):
def callback(message):
return message
self.pubnub.publish(
self.channel,
m,
callback=callback,
error=callback
)
def subscribe(self):
def callback(message, channel):
print(message)
def error(message):
print("ERROR : " + str(message))
def connect(message):
print("CONNECTED")
def reconnect(message):
print("RECONNECTED")
def disconnect(message):
print("DISCONNECTED")
self.pubnub.subscribe(
self.channel,
callback=callback,
error=callback,
connect=connect,
reconnect=reconnect,
disconnect=disconnect
)
示例15: __init__
def __init__(self, args):
self.channel = args.channel
self.pubnub = Pubnub(
args.publish,
args.subscribe,
None,
False
)