diff options
Diffstat (limited to 'python/examples')
| -rw-r--r-- | python/examples/audit.py | 2 | ||||
| -rw-r--r-- | python/examples/console.py | 2 | ||||
| -rw-r--r-- | python/examples/cr.py | 2 | ||||
| -rwxr-xr-x | python/examples/dev-console.py | 2 | ||||
| -rw-r--r-- | python/examples/grant.py | 2 | ||||
| -rw-r--r-- | python/examples/here-now.py | 2 | ||||
| -rw-r--r-- | python/examples/history.py | 14 | ||||
| -rw-r--r-- | python/examples/pam_demo/demo.py | 114 | ||||
| -rw-r--r-- | python/examples/presence.py | 2 | ||||
| -rw-r--r-- | python/examples/publish.py | 4 | ||||
| -rw-r--r-- | python/examples/pubnub-console/pubnub-console | 2 | ||||
| -rw-r--r-- | python/examples/revoke.py | 2 | ||||
| -rw-r--r-- | python/examples/subscribe.py | 9 | ||||
| -rw-r--r-- | python/examples/subscribe_group.py | 2 |
14 files changed, 145 insertions, 16 deletions
diff --git a/python/examples/audit.py b/python/examples/audit.py index ebf31af..c53c2bd 100644 --- a/python/examples/audit.py +++ b/python/examples/audit.py @@ -7,7 +7,7 @@ import sys -from Pubnub import Pubnub +from pubnub import Pubnub publish_key = len(sys.argv) > 1 and sys.argv[1] or 'pam' subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'pam' diff --git a/python/examples/console.py b/python/examples/console.py index bfa4486..a1915ed 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -7,7 +7,7 @@ import sys -from Pubnub import Pubnub +from pubnub import Pubnub import threading from datetime import datetime diff --git a/python/examples/cr.py b/python/examples/cr.py index c537780..f63e6d2 100644 --- a/python/examples/cr.py +++ b/python/examples/cr.py @@ -7,7 +7,7 @@ import sys -from Pubnub import Pubnub +from pubnub import Pubnub publish_key = len(sys.argv) > 1 and sys.argv[1] or 'pam' subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'pam' diff --git a/python/examples/dev-console.py b/python/examples/dev-console.py index 134d2e7..8527307 100755 --- a/python/examples/dev-console.py +++ b/python/examples/dev-console.py @@ -6,7 +6,7 @@ ## http://www.pubnub.com/ import sys -from Pubnub import Pubnub +from pubnub import Pubnub from optparse import OptionParser diff --git a/python/examples/grant.py b/python/examples/grant.py index af9352e..b0832e4 100644 --- a/python/examples/grant.py +++ b/python/examples/grant.py @@ -7,7 +7,7 @@ import sys -from Pubnub import Pubnub +from pubnub import Pubnub publish_key = len(sys.argv) > 1 and sys.argv[1] or 'pam' subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'pam' diff --git a/python/examples/here-now.py b/python/examples/here-now.py index 9640cc5..4c2dc4c 100644 --- a/python/examples/here-now.py +++ b/python/examples/here-now.py @@ -7,7 +7,7 @@ import sys -from Pubnub import Pubnub +from pubnub import Pubnub publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo' subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' diff --git a/python/examples/history.py b/python/examples/history.py index 603a0f8..19593e1 100644 --- a/python/examples/history.py +++ b/python/examples/history.py @@ -7,7 +7,7 @@ import sys -from Pubnub import Pubnub +from pubnub import Pubnub publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo' subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' @@ -33,3 +33,15 @@ def callback(message): print(message) pubnub.history(channel, count=2, callback=callback, error=callback) + +# Synchronous usage + +print pubnub.history(channel, count=2, include_token=True) + +# Asynchronous usage + + +def callback(message): + print(message) + +pubnub.history(channel, count=2, include_token=True, callback=callback, error=callback) diff --git a/python/examples/pam_demo/demo.py b/python/examples/pam_demo/demo.py new file mode 100644 index 0000000..c08f9f5 --- /dev/null +++ b/python/examples/pam_demo/demo.py @@ -0,0 +1,114 @@ +from gevent.monkey import patch_all +patch_all() + +import sys +from pubnub import Pubnub +import random +import json + +rand = str(random.randint(1,99999999)) + +def get_unique(s): + return 'str-' + rand + '-' + s + +# public channel +# This is the channel all clients announce themselves on -- or more generally speaking, a channel you expect the client +# to "check-in" on to announce his state + +#channel_public = get_unique("channel_public") +channel_public = "channel_public" + +# server auth key +# Only the server has/knows about this auth token. It will be used to grant read on the "check-in" Presence channel + +#server_auth_token = get_unique("server_auth_token") +server_auth_token = "server_auth_token" + +# client auth key +# only clients will use this authey -- it does not provide presence channel read access + +#client_auth_token = get_unique("client_auth_token") +client_auth_token = "client_auth_token" + +# each client must have a unique id -- a UUID, for presence information/state to bind to + +# client uuid +client_uuid = get_unique("client_uuid") + +# server uuid +server_uuid = get_unique("server_uuid") + +# For the demo, we'll implement a SERVER called server, who is the authoritative 'admin' entity in the system +# We'll also implement a CLIENT called client, who is an arbitrary hardware device member of the network + +# Please swap out the default 'pam' demo keys with your own PAM-enabled keys + +# init server object +server = Pubnub(publish_key="pam", subscribe_key="pam", secret_key="pam", auth_key=server_auth_token, uuid=server_uuid) + +# init client object +client = Pubnub(publish_key="pam", subscribe_key="pam", auth_key=client_auth_token, uuid=client_uuid) + +# To access a Presence channel with PAM, its format is CHANNELNAME-pnpres + +# Grant permission to server auth keys +# grant r/w to public, and r/w public Presence (public-pnpres) + +print(server.grant(channel=channel_public, auth_key=server_auth_token, read=True, write=True)) +print(server.grant(channel=channel_public + '-pnpres', auth_key=server_auth_token, read=True, write=True)) + +# Grant permission to client auth keys +# grant r/w to public, and w-only access to public Presence (public-pnpres) +print(server.grant(channel=channel_public, auth_key=client_auth_token, read=True, write=False)) +print(server.grant(channel=channel_public + '-pnpres', auth_key=client_auth_token, read=False, write=False)) + +# Now, we'll run it to watch it work as advertised... + +# Define some simple callabcks for the Server and Client + +def _server_message_callback(message, channel): + print("Server heard: " + json.dumps(message)) + +def _client_message_callback(message, channel): + print("Client heard: " + json.dumps(message)) + +def _client_error_callback(error, channel): + print("Client Error: " + error + " on channel " + channel) + print("TTL on grant expired, or token was invalid, or revoked. Client will now unsubscribe from this unauthorized channel.") + client.unsubscribe(channel=channel) + +def _server_error_callback(error, channel): + print("Server Error: " + error + " on channel " + channel) + print("TTL on grant expired, or token was revoked. Server will now unsubscribe from this unauthorized channel.") + server.unsubscribe(channel=channel) + +def _server_presence_callback(message, channel): + print message + if 'action' in message: + if message['action'] == 'join' and message['uuid'] == client_uuid: + print "Server can see that client with UUID " + message['uuid'] + " has a state of " + json.dumps(message['data']) + +def _client_presence_callback(message, channel): + print message + if 'action' in message: + if message['action'] == 'join' and message['uuid'] == client_uuid: + print "Client can see that client with UUID " + message['uuid'] + " has a state of " + json.dumps(message['data']) + +# server subscribes to public channel +server.subscribe(channels=channel_public, callback=_server_message_callback, error=_server_error_callback) + +# server subscribes to presence events on public channel +# presence() is a convienence method that subscribes to channel-pnpres with special logic for handling +# presence-event formatted messages + +## uncomment out to see server able to read on presence channel +server.presence(channel=channel_public, callback=_server_presence_callback, error=_server_error_callback) + +# now if the client tried to subscribe on the presence channel, and therefore, get state info +# he is explicitly denied! + +## uncomment out to see client not able to read on presence channel +client.presence(channel=channel_public, callback=_client_presence_callback, error=_client_error_callback) + +# client subscribes to public channel +client.subscribe(channels=channel_public, state={ "myKey" : get_unique("foo")}, callback=_client_message_callback, error=_client_error_callback) diff --git a/python/examples/presence.py b/python/examples/presence.py index ab91321..7d9af3b 100644 --- a/python/examples/presence.py +++ b/python/examples/presence.py @@ -7,7 +7,7 @@ import sys -from Pubnub import Pubnub +from pubnub import Pubnub publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo' subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' diff --git a/python/examples/publish.py b/python/examples/publish.py index 594e7c4..3bc5e8f 100644 --- a/python/examples/publish.py +++ b/python/examples/publish.py @@ -7,7 +7,7 @@ import sys -from Pubnub import Pubnub +from pubnub import Pubnub publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo' subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' @@ -19,7 +19,7 @@ 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) + secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on, pooling=False) channel = 'hello_world' message = 'Hello World !!!' diff --git a/python/examples/pubnub-console/pubnub-console b/python/examples/pubnub-console/pubnub-console index bfa4486..a1915ed 100644 --- a/python/examples/pubnub-console/pubnub-console +++ b/python/examples/pubnub-console/pubnub-console @@ -7,7 +7,7 @@ import sys -from Pubnub import Pubnub +from pubnub import Pubnub import threading from datetime import datetime diff --git a/python/examples/revoke.py b/python/examples/revoke.py index 437e5b5..9bee010 100644 --- a/python/examples/revoke.py +++ b/python/examples/revoke.py @@ -7,7 +7,7 @@ import sys -from Pubnub import Pubnub +from pubnub import Pubnub publish_key = len(sys.argv) > 1 and sys.argv[1] or 'pam' subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'pam' diff --git a/python/examples/subscribe.py b/python/examples/subscribe.py index 9b8b223..489c7c1 100644 --- a/python/examples/subscribe.py +++ b/python/examples/subscribe.py @@ -7,7 +7,7 @@ import sys -from Pubnub import Pubnub +from pubnub import Pubnub publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo' subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' @@ -19,7 +19,7 @@ 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) + secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on, daemon=True) channel = 'a' @@ -45,5 +45,8 @@ def disconnect(message): print("DISCONNECTED") -pubnub.subscribe(channel, callback=callback, error=callback, +pubnub.subscribe(channels=channel, callback=callback, error=callback, connect=connect, reconnect=reconnect, disconnect=disconnect) +import time +while True: + time.sleep(10) diff --git a/python/examples/subscribe_group.py b/python/examples/subscribe_group.py index ee8e190..c6dcf67 100644 --- a/python/examples/subscribe_group.py +++ b/python/examples/subscribe_group.py @@ -7,7 +7,7 @@ import sys -from Pubnub import Pubnub as Pubnub +from pubnub import Pubnub as Pubnub publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo' subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' |
