本文整理汇总了Python中pubnub.Pubnub.publish方法的典型用法代码示例。如果您正苦于以下问题:Python Pubnub.publish方法的具体用法?Python Pubnub.publish怎么用?Python Pubnub.publish使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pubnub.Pubnub
的用法示例。
在下文中一共展示了Pubnub.publish方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import publish [as 别名]
class Pnub:
def __init__(self, pkey, skey, channel):
# Assign publisher key, subscriber key and channel1
self.pkey = pkey
self.skey = skey
self.channel = channel
# Create pubnub object
self.pubnub = Pubnub(publish_key = self.pkey, subscribe_key = self.skey)
def callback(self, m):
#print(m)
pass
def error(self, m):
print("Pubnub error:", m)
def publish(self, keys, values):
lenght = len(keys)
if(lenght != len(values)):
print("Lists must be of same length!")
return
json_map = {}
for i in range(0, lenght):
json_map[keys[i]] = values[i]
result = json.dumps(json_map)
self.pubnub.publish(self.channel, result, callback = self.callback, error=self.error)
示例2: manageGPSLocation
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import publish [as 别名]
class manageGPSLocation(Thread) :
test = os.environ
pubnub_publish_key = os.environ['PUBNUB_PUBLISH_KEY']
pubnub_subscribe_key = os.environ['PUBNUB_SUBSCRIBE_KEY']
mongodb_connection = None
collection = None
def __init__(self):
Thread.__init__(self)
mongodb_connection = ConnectionToDatabase()
manageGPSLocation.collection = mongodb_connection.getCollection("steeds")
self.pubnub_settings = Pubnub(publish_key=manageGPSLocation.pubnub_publish_key,subscribe_key=manageGPSLocation.pubnub_subscribe_key)
# Rename to location channel
self.pubnub_channel = "channel_test"
self.genericDAO = GenericDAO()
def subscriber_callback(self, message, channel):
print(message)
criteriaDict = {}
criteriaDict["_id"]=message["_id"]
updateDict = {}
subUpdateDict = {}
subUpdateDict["type"]="Point"
subUpdateDict["coordinates"] = [message["lng"],message["lat"]]
updateDict["location"]=subUpdateDict
self.genericDAO.updateObjects(manageGPSLocation.collection, criteriaDict, updateDict)
def subscriber_error(self, message):
print("ERROR : "+message)
def connect(self, message):
print("CONNECTED TO LOCATION CHANNEL")
def reconnect(self, message):
print("RECONNECTED TO LOCATION CHANNEL")
def disconnect(self, message):
print("DISCONNECTED FROM LOCATION CHANNEL")
def subscribe(self):
# souscrire au channel
self.pubnub_settings.subscribe(channels=self.pubnub_channel, callback=self.subscriber_callback, error=self.subscriber_error
,connect=self.connect, reconnect=self.reconnect, disconnect=self.disconnect)
def publish(self, message):
self.pubnub_settings.publish(channel=self.pubnub_channel, message=message)
def unsubscribe(self):
# se desinscire du channel
self.pubnub_settings.unsubscribe(self.pubnub_channel)
示例3: notify
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import publish [as 别名]
def notify(msg):
pubnub = Pubnub(
publish_key=os.getenv('PUBLISH_KEY'),
subscribe_key=os.getenv('SUBSCRIBE_KEY'),
pooling=False
)
channel = 'service_channel'
# Synchronous usage
print pubnub.publish(channel, msg)
示例4: send_to_channel
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import publish [as 别名]
def send_to_channel(ch,name,obj):
from pubnub import Pubnub
pubnub = Pubnub(publish_key, subscribe_key)
s=json.dumps(obj)
dl='-_-_'+str(len(s)+1000000)[1:]
s=s+dl
logging.warning(str(len(s)-10)+" "+ str(len(urllib.quote(s.encode("utf-8")))+97)) #pana a 16000…;la 18000 da err
page_num=name
for i in xrange(0, len(s), 12000):
pubnub.publish(ch, name+page_num+str(i+1000000)[1:]+s[i:i+12000], callback= _callback, error= _callback)
示例5: __init__
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import publish [as 别名]
class Client:
command_key = "command"
def __init__(self, channel):
self.channel = channel
# Publish key is the one that usually starts with the "pub-c-" prefix
publish_key = "pub-c-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
# Subscribe key is the one that usually starts with the "sub-c" prefix
# Do not forget to replace the string with your subscribe key
subscribe_key = "sub-c-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
self.pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key)
self.pubnub.subscribe(channels=self.channel,
callback=self.callback,
error=self.callback,
connect=self.connect,
reconnect=self.reconnect,
disconnect=self.disconnect)
def callback_command_message(self, message):
print("I've received the following response from PubNub cloud: {0}".format(message))
def error_command_message(self, message):
print("There was an error when working with the PubNub cloud: {0}".format(message))
def publish_command(self, command_name, key, value):
command_message = {
self.__class__.command_key: command_name,
key: value}
self.pubnub.publish(
channel=self.channel,
message=command_message,
callback=self.callback_command_message,
error=self.error_command_message)
def callback(self, message, channel):
if channel == self.channel:
print("I've received the following message: {0}".format(message))
def error(self, message):
print("Error: " + str(message))
def connect(self, message):
print("Connected to the {0} channel".
format(self.channel))
print(self.pubnub.publish(
channel=self.channel,
message="Listening to messages in the PubNub Python Client"))
def reconnect(self, message):
print("Reconnected to the {0} channel".
format(self.channel))
def disconnect(self, message):
print("Disconnected from the {0} channel".
format(self.channel))
示例6: manageClients
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import publish [as 别名]
class manageClients(Thread) :
test = os.environ
pubnub_publish_key = os.environ['PUBNUB_PUBLISH_KEY']
pubnub_subscribe_key = os.environ['PUBNUB_SUBSCRIBE_KEY']
mongodb_connection = None
collection = None
def __init__(self):
Thread.__init__(self)
mongodb_connection = ConnectionToDatabase()
manageClients.deliveries_collection = mongodb_connection.getCollection("temp_deliveries")
self.pubnub_settings = Pubnub(publish_key=manageClients.pubnub_publish_key,subscribe_key=manageClients.pubnub_subscribe_key)
# Rename to location channel
self.pubnub_channel = "clients_channel"
self.genericDAO = GenericDAO()
self.manageSteeds = manageSteeds()
def subscriber_callback(self, message, channel):
print(message)
# Inserer la demande de livraison faite par le client
if(message["msg_code"] == "XX") :
# inserer la demande de livraison dans une collection temporaire
id_delivery = self.genericDAO.insertObject(manageClients.deliveries_collection, message)
def subscriber_error(self, message):
print("ERROR : "+message)
def connect(self, message):
print("CONNECTED TO LOCATION CHANNEL")
def reconnect(self, message):
print("RECONNECTED TO LOCATION CHANNEL")
def disconnect(self, message):
print("DISCONNECTED FROM LOCATION CHANNEL")
def subscribe(self):
# souscrire au channel
self.pubnub_settings.subscribe(channels=self.pubnub_channel, callback=self.subscriber_callback, error=self.subscriber_error
,connect=self.connect, reconnect=self.reconnect, disconnect=self.disconnect)
def publish(self, message):
self.pubnub_settings.publish(channel=self.pubnub_channel, message=message)
def unsubscribe(self):
# se desinscire du channel
self.pubnub_settings.unsubscribe(self.pubnub_channel)
示例7: ImagePublisher
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import publish [as 别名]
class ImagePublisher(object):
def __init__(self):
self.pubnub = Pubnub(
publish_key=PUBNUB_PUBLISH_KEY,
subscribe_key=PUBNUB_SUBSCRIBE_KEY,
cipher_key='',
ssl_on=False)
def publish_frame(self, jpeg_frame):
message = base64.b64encode(jpeg_frame)
self.pubnub.publish(PUBLISH_CHANNEL, message)
示例8: main
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import publish [as 别名]
def main():
publish_key = os.environ['PUBNUB_PUBLISH_KEY']
subscribe_key = os.environ['PUBNUB_SUBSCRIBE_KEY']
pubnub = Pubnub(publish_key, subscribe_key, ssl_on=False)
try:
cpu = system.SystemInfo()
while True:
pubnub.publish(channel="kiettv.raspberry.os", message=cpu.get_cpu_percent(), callback=None, error=callback)
time.sleep(1)
except KeyboardInterrupt:
pubnub.unsubscribe(channel='kiettv.raspberry.os')
exit(0)
示例9: _send_image_back_to_user
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import publish [as 别名]
def _send_image_back_to_user(phone_num_or_email, watermark_image_url):
if COMPANY_SETTINGS["web_flow"]:
# TODO vars need renaming
pubnub = Pubnub(
publish_key=PUBNUB_PUBLISH_KEY,
subscribe_key=PUBNUB_SUBSCRIBE_KEY,
)
pubnub.publish(phone_num_or_email, watermark_image_url)
elif COMPANY_SETTINGS["sms_photos"]:
ImageTexter().text_image(phone_num_or_email, watermark_image_url)
elif COMPANY_SETTINGS["email_photos"]:
ImageEmailer().email_image(phone_num_or_email, watermark_image_url)
示例10: PiBusHub
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import publish [as 别名]
class PiBusHub(object):
def __init__(self):
self.logger = logging.getLogger(__name__)
if not hasattr(cfg, 'PUBNUB_CHANNEL_KEY'):
self.logger.debug("PubNub[ChannelKey] not defined - PubNub support disabled")
if not hasattr(cfg, 'PUBNUB_PUBLISH_KEY'):
self.logger.debug("PubNub[PublishKey] not defined - PubNub support disabled")
return
if cfg.PUBNUB_PUBLISH_KEY == '':
self.logger.debug("PubNub[PublishKey] not configured - PubNub support disabled")
return
if not hasattr(cfg, 'PUBNUB_SUBSCRIBE_KEY'):
self.logger.debug("PubNub[SubscribeKey] not defined - PubNub support disabled")
return
self.logger.debug("PubNub registering: " +cfg.PUBNUB_PUBLISH_KEY + " : " + cfg.PUBNUB_SUBSCRIBE_KEY)
self.pubnub = Pubnub(publish_key=cfg.PUBNUB_PUBLISH_KEY, subscribe_key=cfg.PUBNUB_SUBSCRIBE_KEY)
if cfg.PUBNUB_SUBSCRIBE_KEY != '':
self.logger.info("PubNub subscribing: " + cfg.PUBNUB_CHANNEL_KEY)
self.pubnub.subscribe(channels=cfg.PUBNUB_CHANNEL_KEY, callback=self.callback, error=self.error, connect=self.connect, reconnect=self.reconnect, disconnect=self.disconnect)
def callback(self, message, channel):
print("CALLBACK: " + message +":" + channel)
def error(message):
print("ERROR : " + str(message))
def connect(self, channel):
print("CONNECTED: " +channel)
self.pubnub.publish(channel='Mongo', message='Hello from the PubNub Python SDK')
def reconnect(message):
print("RECONNECTED:" + message)
def disconnect(message):
print("DISCONNECTED:" + message)
def publish(self, message):
print("PUBLISH:" + message)
self.pubnub.publish(channel=cfg.PUBNUB_CHANNEL_KEY, message=message)
示例11: MyPubnubConn
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import publish [as 别名]
class MyPubnubConn():
def __init__(self, channel):
self.conn = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key, ssl_on=True,
# cipherkey randomly generated for purposes of PoC, but it is too short and needs to be
# securely stored.
# cipher_key=cipher_key
)
self.channel = channel
self.subscribed = threading.Event()
# server doesn't need to subscribe, only publish
#self.conn.subscribe(channels=self.channel, callback=self.incoming_message, error=self.error_cb,
# connect=self.connect_cb, reconnect=self.reconnect_cb, disconnect=self.disconnect_cb)
def incoming_message(self, message, channel):
pass
#print(message)
def error_cb(self, message):
pass
#print("\tERROR : " + str(message))
def connect_cb(self, message):
self.subscribed.set()
def reconnect_cb(self, message):
self.subscribed.set()
#print("\tRECONNECTED")
def disconnect_cb(self, message):
self.subscribed.clear()
#print("\tDISCONNECTED")
def send_cb(self, message):
pass
def discontinue(self):
self.conn.unsubscribe(channel=self.channel)
def publish(self, message):
self.conn.publish(channel=self.channel, message=message, callback=self.send_cb, error=self.error_cb)
def wait_until_ready(self):
self.subscribed.wait()
def print_status(self):
print("\nCurrently connected to channel '%s'" % self.channel)
示例12: send_message
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import publish [as 别名]
def send_message(app, message):
app.logger.debug("Publish message {} to channel {}".format(message, PUBNUB_CHANNEL))
client = Pubnub(publish_key=app.config['PUBNUB_PUBLISH'],
subscribe_key=app.config['PUBNUB_SUBSCRIBER'])
return client.publish(channel=PUBNUB_CHANNEL,
message=message)
示例13: __init__
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import publish [as 别名]
class WorkspaceSensor:
def __init__(self,setting_file_path):
with open(setting_file_path) as f:
self._setting = json.load(f,"utf-8")
self._circuit = circuit.parse(self._setting["GPIO"])
self._current_status = self._circuit.get_status()
self._on_resistance = Resistance(self._setting["on_resistance"],1,self._update_status)
self._off_resistance = Resistance(self._setting["off_resistance"],0,self._update_status)
self._pubnub = Pubnub(publish_key=self._setting["publish_key"], subscribe_key=self._setting["subscribe_key"])
self._pubnub.subscribe(channels='plzcast_' + self._setting["group"], callback=self._callback_plzcast,connect=self._callback_connect, reconnect=self._callback_connect)
def run(self):
while True:
sensor_status = self._circuit.get_status()
if(sensor_status == 1):
self._on_resistance.load()
self._off_resistance.clear()
else:
self._off_resistance.load()
self._on_resistance.clear()
time.sleep(self._setting["duration"])
def _send(self):
message=json.dumps({
"floor":self._setting["floor"],
"id":self._setting["id"],
"name":self._setting["name"],
"status":self._current_status
})
self._pubnub.publish(channel='wkstatus_' + self._setting["group"],message=message)
def _callback_plzcast(self,message,channel):
self._send()
def _callback_connect(self,message):
self._send()
def _update_status(self,new_status):
if(new_status != self._current_status):
self._current_status = new_status
self._send()
示例14: iotbridge
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import publish [as 别名]
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)
示例15: callback
# 需要导入模块: from pubnub import Pubnub [as 别名]
# 或者: from pubnub.Pubnub import publish [as 别名]
class Net:
def callback(self, message):
print(message)
def __init__(self, channel = "Channel-ux8kwdv1u"):
self.channel = channel
self.pubnub = Pubnub(publish_key=PUB_KEY,
subscribe_key=SUB_KEY)
def subscribe(self, callback):
self.pubnub.subscribe(channels=self.channel,
callback=callback, error=self.callback)
def unsubscribe(self):
self.pubnub.unsubscribe(channel=self.channel)
def publish(self, message):
self.pubnub.publish(channel=self.channel, message=message,
callback=self.callback, error=self.callback)