aboutsummaryrefslogtreecommitdiffstats
path: root/python/examples
diff options
context:
space:
mode:
Diffstat (limited to 'python/examples')
-rwxr-xr-xpython/examples/dev-console.py106
-rw-r--r--python/examples/here-now-example.py26
-rwxr-xr-xpython/examples/history-example.py7
-rwxr-xr-xpython/examples/publish-example.py52
-rwxr-xr-xpython/examples/subscribe-example.py34
5 files changed, 137 insertions, 88 deletions
diff --git a/python/examples/dev-console.py b/python/examples/dev-console.py
index 383fa68..7814cef 100755
--- a/python/examples/dev-console.py
+++ b/python/examples/dev-console.py
@@ -1,4 +1,4 @@
-## www.pubnub.com - PubNub Real-time push service in the cloud.
+## www.pubnub.com - PubNub Real-time push service in the cloud.
# coding=utf8
## PubNub Real-time Push APIs and Notifications Framework
@@ -55,41 +55,55 @@ parser.add_option("--uuid",
print(options)
-pubnub = Pubnub(options.publish_key, options.subscribe_key, options.secret_key, options.cipher_key, options.auth_key, options.ssl, options.origin, options.uuid)
+pubnub = Pubnub(options.publish_key,
+ options.subscribe_key,
+ options.secret_key,
+ options.cipher_key,
+ options.auth_key,
+ options.ssl,
+ options.origin,
+ options.uuid)
class color:
- PURPLE = '\033[95m'
- CYAN = '\033[96m'
- DARKCYAN = '\033[36m'
- BLUE = '\033[94m'
- GREEN = '\033[92m'
- YELLOW = '\033[93m'
- RED = '\033[91m'
- BOLD = '\033[1m'
- UNDERLINE = '\033[4m'
- END = '\033[0m'
+ PURPLE = '\033[95m'
+ CYAN = '\033[96m'
+ DARKCYAN = '\033[36m'
+ BLUE = '\033[94m'
+ GREEN = '\033[92m'
+ YELLOW = '\033[93m'
+ RED = '\033[91m'
+ BOLD = '\033[1m'
+ UNDERLINE = '\033[4m'
+ END = '\033[0m'
from datetime import datetime
+
def print_ok(msg, channel=None):
- chstr = color.PURPLE + "[" + datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "] " + color.END
- chstr += color.CYAN + "[Channel : " + channel + "] " if channel is not None else "" + color.END
+ chstr = color.PURPLE + "[" + datetime.now().strftime(
+ '%Y-%m-%d %H:%M:%S') + "] " + color.END
+ chstr += color.CYAN + "[Channel : " + channel + \
+ "] " if channel is not None else "" + color.END
try:
- print(chstr + color.GREEN + str(msg) + color.END)
+ print(chstr + color.GREEN + str(msg) + color.END)
except Exception as e:
print(msg)
+
def print_error(msg, channel=None):
- chstr = color.PURPLE + "[" + datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "] " + color.END
- chstr += color.CYAN + "[Channel : " + channel + "] " if channel is not None else "" + color.END
+ chstr = color.PURPLE + "[" + datetime.now().strftime(
+ '%Y-%m-%d %H:%M:%S') + "] " + color.END
+ chstr += color.CYAN + "[Channel : " + channel + \
+ "] " if channel is not None else "" + color.END
try:
- print( chstr + color.RED + color.BOLD +str(msg) + color.END)
+ print(chstr + color.RED + color.BOLD + str(msg) + color.END)
except:
print(msg)
import threading
+
def kill_all_threads():
for thread in threading.enumerate():
if thread.isAlive():
@@ -101,6 +115,7 @@ def kill_all_threads():
#thread.exit()
#print(str(thread.getName()) + ' could not be terminated')
+
def get_input(message, t=None):
while True:
try:
@@ -117,7 +132,8 @@ def get_input(message, t=None):
raise ValueError
if t is not None and t == bool:
- if command in ["True", "true", "1", 1, "y", "Y", "yes", "Yes", "YES"]:
+ valid = ["True", "true", "1", 1, "y", "Y", "yes", "Yes", "YES"]
+ if command in valid:
return True
else:
return False
@@ -131,18 +147,20 @@ def get_input(message, t=None):
print_error("Invalid input : " + command)
-
def _publish_command_handler():
channel = get_input("[PUBLISH] Enter Channel Name ", str)
if channel is None:
return
while True:
- message = get_input("[PUBLISH] Enter Message ( QUIT or CTRL-C for exit from publish mode ) ")
- if message == 'QUIT' or message == 'quit' or message == None:
- return
+ message = get_input("[PUBLISH] Enter Message \
+ ( QUIT or CTRL-C for exit from publish mode ) ")
+ if message == 'QUIT' or message == 'quit' or message is None:
+ return
+
def _callback(r):
print_ok(r)
+
def _error(r):
print_error(r)
pubnub.publish(channel, message, _callback, _error)
@@ -150,23 +168,30 @@ def _publish_command_handler():
def _subscribe_command_handler():
channel = get_input("[SUBSCRIBE] Enter Channel Name ", str)
+
def _callback(r):
print_ok(r, channel)
+
def _error(r):
print_error(r, channel)
pubnub.subscribe(channel, _callback, _error)
+
def _unsubscribe_command_handler():
channel = get_input("[UNSUBSCRIBE] Enter Channel Name ", str)
+
def _callback(r):
print_ok(r)
+
def _error(r):
print_error(r)
pubnub.unsubscribe(channel)
+
def _grant_command_handler():
def _callback(r):
print_ok(r)
+
def _error(r):
print_error(r)
channel = get_input("[GRANT] Enter Channel Name ", str)
@@ -174,11 +199,13 @@ def _grant_command_handler():
ttl = get_input("[GRANT] Enter ttl ", int)
read = get_input("[GRANT] Read ? ", bool)
write = get_input("[GRANT] Write ? ", bool)
- pubnub.grant(channel, auth_key,read,write,ttl, _callback)
+ pubnub.grant(channel, auth_key, read, write, ttl, _callback)
+
def _revoke_command_handler():
def _callback(r):
print_ok(r)
+
def _error(r):
print_error(r)
channel = get_input("[REVOKE] Enter Channel Name ", str)
@@ -187,18 +214,22 @@ def _revoke_command_handler():
pubnub.revoke(channel, auth_key, ttl, _callback)
+
def _audit_command_handler():
def _callback(r):
print_ok(r)
+
def _error(r):
print_error(r)
channel = get_input("[AUDIT] Enter Channel Name ", str)
auth_key = get_input("[AUDIT] Enter Auth Key ", str)
pubnub.audit(channel, auth_key, _callback)
+
def _history_command_handler():
def _callback(r):
print_ok(r)
+
def _error(r):
print_error(r)
channel = get_input("[HISTORY] Enter Channel Name ", str)
@@ -210,6 +241,7 @@ def _history_command_handler():
def _here_now_command_handler():
def _callback(r):
print_ok(r)
+
def _error(r):
print_error(r)
channel = get_input("[HERE NOW] Enter Channel Name ", str)
@@ -218,28 +250,32 @@ def _here_now_command_handler():
commands = []
-commands.append({"command" : "publish", "handler" : _publish_command_handler})
-commands.append({"command" : "subscribe", "handler" : _subscribe_command_handler})
-commands.append({"command" : "unsubscribe", "handler" : _unsubscribe_command_handler})
-commands.append({"command" : "here_now", "handler" : _here_now_command_handler})
-commands.append({"command" : "history", "handler" : _history_command_handler})
-commands.append({"command" : "grant", "handler" : _grant_command_handler})
-commands.append({"command" : "revoke", "handler" : _revoke_command_handler})
-commands.append({"command" : "audit", "handler" : _audit_command_handler})
+commands.append({"command": "publish", "handler": _publish_command_handler})
+commands.append(
+ {"command": "subscribe", "handler": _subscribe_command_handler})
+commands.append(
+ {"command": "unsubscribe", "handler": _unsubscribe_command_handler})
+commands.append(
+ {"command": "here_now", "handler": _here_now_command_handler})
+commands.append({"command": "history", "handler": _history_command_handler})
+commands.append({"command": "grant", "handler": _grant_command_handler})
+commands.append({"command": "revoke", "handler": _revoke_command_handler})
+commands.append({"command": "audit", "handler": _audit_command_handler})
# last command is quit. add new commands before this line
-commands.append({"command" : "QUIT"})
+commands.append({"command": "QUIT"})
+
def get_help():
help = ""
help += "Channels currently subscribed to : "
help += str(pubnub.get_channel_array())
help += "\n"
- for i,v in enumerate(commands):
+ for i, v in enumerate(commands):
help += "Enter " + str(i) + " for " + v['command'] + "\n"
return help
-
+
while True:
command = get_input(color.BLUE + get_help(), int)
if command == len(commands) - 1 or command is None:
diff --git a/python/examples/here-now-example.py b/python/examples/here-now-example.py
index b9f0b02..0888410 100644
--- a/python/examples/here-now-example.py
+++ b/python/examples/here-now-example.py
@@ -1,4 +1,4 @@
-## www.pubnub.com - PubNub Real-time push service in the cloud.
+## www.pubnub.com - PubNub Real-time push service in the cloud.
# coding=utf8
## PubNub Real-time Push APIs and Notifications Framework
@@ -14,24 +14,24 @@ sys.path.append('../')
sys.path.append('./')
from Pubnub import Pubnub
-publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'
+publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'
subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo'
-secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo'
-cipher_key = len(sys.argv) > 4 and sys.argv[4] or ''
-ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False
+secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo'
+cipher_key = len(sys.argv) > 4 and sys.argv[4] or ''
+ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False
## -----------------------------------------------------------------------
## Initiate Pubnub State
## -----------------------------------------------------------------------
-pubnub = Pubnub( publish_key=publish_key, subscribe_key=subscribe_key,
- secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on)
-crazy = 'hello_world'
+pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key,
+ secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on)
+crazy = 'hello_world'
+
def print_cb(message):
- print(message)
+ print(message)
-pubnub.here_now( {
- 'channel' : crazy,
- 'callback' : print_cb
+pubnub.here_now({
+ 'channel': crazy,
+ 'callback': print_cb
})
-
diff --git a/python/examples/history-example.py b/python/examples/history-example.py
index bf78c7d..b5cc1fd 100755
--- a/python/examples/history-example.py
+++ b/python/examples/history-example.py
@@ -4,12 +4,11 @@ sys.path.append('./')
from Pubnub import Pubnub
## Initiat Class
-pubnub = Pubnub( 'demo', 'demo', None, False )
+pubnub = Pubnub('demo', 'demo', None, False)
## History Example
history = pubnub.history({
- 'channel' : 'hello_world',
- 'limit' : 1
+ 'channel': 'hello_world',
+ 'limit': 1
})
print(history)
-
diff --git a/python/examples/publish-example.py b/python/examples/publish-example.py
index e632aef..ebf3c73 100755
--- a/python/examples/publish-example.py
+++ b/python/examples/publish-example.py
@@ -1,4 +1,4 @@
-## www.pubnub.com - PubNub Real-time push service in the cloud.
+## www.pubnub.com - PubNub Real-time push service in the cloud.
# coding=utf8
## PubNub Real-time Push APIs and Notifications Framework
@@ -15,54 +15,62 @@ sys.path.append('../')
sys.path.append('../../')
from Pubnub import Pubnub
-publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'
+publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'
subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo'
-secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo'
-cipher_key = len(sys.argv) > 4 and sys.argv[4] or '' ##(Cipher key is Optional)
-auth_key = len(sys.argv) > 5 and sys.argv[4] or 'abcd' ##(Cipher key is Optional)
-ssl_on = len(sys.argv) > 6 and bool(sys.argv[5]) or False
+secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo'
+cipher_key = len(
+ sys.argv) > 4 and sys.argv[4] or '' # (Cipher key is Optional)
+auth_key = len(
+ sys.argv) > 5 and sys.argv[4] or 'abcd' # (Cipher key is Optional)
+ssl_on = len(sys.argv) > 6 and bool(sys.argv[5]) or False
## -----------------------------------------------------------------------
## Initiate Pubnub State
## -----------------------------------------------------------------------
-pubnub = Pubnub( publish_key, subscribe_key, secret_key, cipher_key, auth_key, ssl_on )
-crazy = 'hello_world'
+pubnub = Pubnub(
+ publish_key, subscribe_key, secret_key, cipher_key, auth_key, ssl_on)
+crazy = 'hello_world'
## -----------------------------------------------------------------------
## Publish Example
## -----------------------------------------------------------------------
+
+
def publish_complete(info):
print(info)
+
def publish_error(info):
- print('ERROR : ' + str(info))
+ print('ERROR : ' + str(info))
## Publish string
pubnub.publish({
- 'channel' : crazy,
- 'message' : 'Hello World!',
- 'callback' : publish_complete,
- 'error' : publish_error
+ 'channel': crazy,
+ 'message': 'Hello World!',
+ 'callback': publish_complete,
+ 'error': publish_error
})
## Publish list
-li = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
+li = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
+ 'Saturday']
pubnub.publish({
- 'channel' : crazy,
- 'message' : li,
- 'callback' : publish_complete,
- 'error' : publish_error
+ 'channel': crazy,
+ 'message': li,
+ 'callback': publish_complete,
+ 'error': publish_error
})
+
def done_cb(info):
publish_complete(info)
pubnub.publish({
- 'channel' : crazy,
- 'message' : { 'some_key' : 'some_val' },
- 'callback' : done_cb,
- 'error' : publish_error
+ 'channel': crazy,
+ 'message': {'some_key': 'some_val'},
+ 'callback': done_cb,
+ 'error': publish_error
})
diff --git a/python/examples/subscribe-example.py b/python/examples/subscribe-example.py
index a67a08f..0a18899 100755
--- a/python/examples/subscribe-example.py
+++ b/python/examples/subscribe-example.py
@@ -8,48 +8,55 @@ import string
from Pubnub import Pubnub
## Initiate Class
-pubnub = Pubnub( 'demo', 'demo', None, False )
+pubnub = Pubnub('demo', 'demo', None, False)
-print("My UUID is: "+pubnub.uuid)
+print("My UUID is: " + pubnub.uuid)
-channel = ''.join(random.choice(string.ascii_letters + string.digits) for x in range(20))
+channel = ''.join(
+ random.choice(string.ascii_letters + string.digits) for x in range(20))
## Subscribe Example
-def receive(message) :
+
+
+def receive(message):
print(message)
return False
+
def pres_event(message):
print(message)
return False
+
def subscribe():
print("Listening for messages on '%s' channel..." % channel)
pubnub.subscribe({
- 'channel' : channel,
- 'callback' : receive
+ 'channel': channel,
+ 'callback': receive
})
+
def presence():
print("Listening for presence events on '%s' channel..." % channel)
pubnub.presence({
- 'channel' : channel,
- 'callback' : pres_event
+ 'channel': channel,
+ 'callback': pres_event
})
+
def publish():
print("Publishing a test message on '%s' channel..." % channel)
pubnub.publish({
- 'channel' : channel,
- 'message' : { 'text':'foo bar' }
+ 'channel': channel,
+ 'message': {'text': 'foo bar'}
})
pres_thread = threading.Thread(target=presence)
-pres_thread.daemon=True
+pres_thread.daemon = True
pres_thread.start()
sub_thread = threading.Thread(target=subscribe)
-sub_thread.daemon=True
+sub_thread.daemon = True
sub_thread.start()
time.sleep(3)
@@ -60,7 +67,6 @@ publish()
print("waiting for subscribes and presence")
pres_thread.join()
-print(pubnub.here_now({'channel':channel}))
+print(pubnub.here_now({'channel': channel}))
sub_thread.join()
-