diff options
| author | Devendra | 2015-06-25 11:42:00 +0530 |
|---|---|---|
| committer | Devendra | 2015-06-25 11:42:00 +0530 |
| commit | ad89de8312f3f407150c7eb411cbf02765910e53 (patch) | |
| tree | 6f683a1d154a4d4f3bb11ed6f2d56ec813db6ccb | |
| parent | 4a44c563ea8af7211197d166596be41ede05c179 (diff) | |
| parent | 232f7389274f0d9ff06835fc5da0970f7918ba25 (diff) | |
| download | pubnub-python-ad89de8312f3f407150c7eb411cbf02765910e53.tar.bz2 | |
Merge branch 'develop'
67 files changed, 394 insertions, 1090 deletions
@@ -5,7 +5,7 @@ ## Installation ``` -pip install pubnub==3.7.1 +pip install pubnub==3.7.2 ``` Examples and instructions for the SDK are available in their acompanying README.md, migration.md and examples directories under their specific platform directories: @@ -1 +1 @@ -3.7.1 +3.7.2 diff --git a/common/unit-test-async.py b/common/unit-test-async.py deleted file mode 100644 index c4dfb65..0000000 --- a/common/unit-test-async.py +++ /dev/null @@ -1,155 +0,0 @@ -## www.pubnub.com - PubNub Real-time push service in the cloud. -# coding=utf8 - -## PubNub Real-time Push APIs and Notifications Framework -## Copyright (c) 2010 Stephen Blum -## http://www.pubnub.com/ - -## ----------------------------------- -## PubNub 3.1 Real-time Push Cloud API -## ----------------------------------- - -import sys -import time -import random -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' -secret_key = len(sys.argv) > 3 and sys.argv[3] or None -cipher_key = len(sys.argv) > 4 and sys.argv[4] or None -ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False - -## ----------------------------------------------------------------------- -## Initiat Class -## ----------------------------------------------------------------------- -pubnub = Pubnub(publish_key, subscribe_key, secret_key, cipher_key, ssl_on) -ch = 'python-async-test-channel-' -expect = 0 -done = 0 -failures = 0 -passes = 0 - - -def stop(): - global done - global count - pubnub.stop() - print "============================" - print 'Total\t:\t', failures + passes - print 'PASS\t:\t', passes - print 'FAIL\t:\t', failures - print "============================" - -## --------------------------------------------------------------------------- -## Unit Test Function -## --------------------------------------------------------------------------- - - -def test(trial, name): - global failures - global passes - global done - done += 1 - #print trial - if trial is False: - print 'FAIL : ', name - failures += 1 - else: - print 'PASS : ', name - passes += 1 - if done == expect: - stop() - - -def test_publish(): - channel = ch + str(random.random()) - - def publish_cb(messages): - test(messages[0] == 1, "Publish Test") - - pubnub.publish({ - 'channel': channel, - 'message': {'one': 'Hello World! --> ɂ顶@#$%^&*()!', 'two': 'hello2'}, - 'callback': publish_cb - }) - - -def test_history(): - channel = ch + str(random.random()) - - def history_cb(messages): - test(len(messages) <= 1, "History Test") - pubnub.history({ - 'channel': channel, - 'limit': 1, - 'callback': history_cb - }) - - -def test_subscribe(): - message = "Testing Subscribe " + str(random.random()) - channel = ch + str(random.random()) - - def subscribe_connect_cb(): - def publish_cb(response): - test(response[0] == 1, - 'Publish Test in subscribe Connect Callback') - pubnub.publish({ - 'channel': channel, - 'message': message, - 'callback': publish_cb - }) - - def subscribe_cb(response): - test(response == message, - 'Subscribe Receive Test in subscribe Callback') - pubnub.subscribe({ - 'channel': channel, - 'connect': subscribe_connect_cb, - 'callback': subscribe_cb - }) - - -def test_here_now(): - channel = ch + str(random.random()) - message = "Testing Subscribe" - - def subscribe_connect_cb(): - def here_now_cb(response): - test(response["occupancy"] > 0, 'Here Now Test') - - def publish_cb(response): - test(response[0] == 1, - 'Here Now Test: Publish Test in \ - subscribe Connect Callback') - pubnub.publish({ - 'channel': channel, - 'message': message, - 'callback': publish_cb - }) - time.sleep(5) - pubnub.here_now({ - 'channel': channel, - 'callback': here_now_cb - }) - - def subscribe_cb(response): - test(response == message, - 'Here Now Test: Subscribe Receive Test in subscribe Callback') - pubnub.subscribe({ - 'channel': channel, - 'connect': subscribe_connect_cb, - 'callback': subscribe_cb - }) - -expect = 7 -test_publish() -test_history() -test_subscribe() -test_here_now() - - -pubnub.start() -if failures > 0: - raise Exception('Fail', failures) diff --git a/docs/build/doctrees/environment.pickle b/docs/build/doctrees/environment.pickle Binary files differindex e9cef6b..1294b22 100644 --- a/docs/build/doctrees/environment.pickle +++ b/docs/build/doctrees/environment.pickle diff --git a/docs/build/html/genindex.html b/docs/build/html/genindex.html index e85fcb8..d812458 100644 --- a/docs/build/html/genindex.html +++ b/docs/build/html/genindex.html @@ -7,7 +7,7 @@ <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> - <title>Index — PubNub 3.7.1 documentation</title> + <title>Index — PubNub 3.7.2 documentation</title> <link rel="stylesheet" href="_static/default.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> @@ -15,7 +15,7 @@ <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: './', - VERSION: '3.7.1', + VERSION: '3.7.2', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true @@ -24,7 +24,7 @@ <script type="text/javascript" src="_static/jquery.js"></script> <script type="text/javascript" src="_static/underscore.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> - <link rel="top" title="PubNub 3.7.1 documentation" href="index.html" /> + <link rel="top" title="PubNub 3.7.2 documentation" href="index.html" /> </head> <body> <div class="related"> @@ -36,7 +36,7 @@ <li class="right" > <a href="py-modindex.html" title="Python Module Index" >modules</a> |</li> - <li><a href="index.html">PubNub 3.7.1 documentation</a> »</li> + <li><a href="index.html">PubNub 3.7.2 documentation</a> »</li> </ul> </div> @@ -486,7 +486,7 @@ <li class="right" > <a href="py-modindex.html" title="Python Module Index" >modules</a> |</li> - <li><a href="index.html">PubNub 3.7.1 documentation</a> »</li> + <li><a href="index.html">PubNub 3.7.2 documentation</a> »</li> </ul> </div> <div class="footer"> diff --git a/docs/build/html/index.html b/docs/build/html/index.html index 0ec9d4c..78ea76c 100644 --- a/docs/build/html/index.html +++ b/docs/build/html/index.html @@ -6,7 +6,7 @@ <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> - <title>Welcome to PubNub’s documentation! — PubNub 3.7.1 documentation</title> + <title>Welcome to PubNub’s documentation! — PubNub 3.7.2 documentation</title> <link rel="stylesheet" href="_static/default.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> @@ -14,7 +14,7 @@ <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: './', - VERSION: '3.7.1', + VERSION: '3.7.2', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true @@ -23,7 +23,7 @@ <script type="text/javascript" src="_static/jquery.js"></script> <script type="text/javascript" src="_static/underscore.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> - <link rel="top" title="PubNub 3.7.1 documentation" href="#" /> + <link rel="top" title="PubNub 3.7.2 documentation" href="#" /> </head> <body> <div class="related"> @@ -35,7 +35,7 @@ <li class="right" > <a href="py-modindex.html" title="Python Module Index" >modules</a> |</li> - <li><a href="#">PubNub 3.7.1 documentation</a> »</li> + <li><a href="#">PubNub 3.7.2 documentation</a> »</li> </ul> </div> @@ -2648,7 +2648,7 @@ Required argument when working with twisted or tornado .</dd> <li class="right" > <a href="py-modindex.html" title="Python Module Index" >modules</a> |</li> - <li><a href="#">PubNub 3.7.1 documentation</a> »</li> + <li><a href="#">PubNub 3.7.2 documentation</a> »</li> </ul> </div> <div class="footer"> diff --git a/docs/build/html/objects.inv b/docs/build/html/objects.inv Binary files differindex 865be47..451dd19 100644 --- a/docs/build/html/objects.inv +++ b/docs/build/html/objects.inv diff --git a/docs/build/html/py-modindex.html b/docs/build/html/py-modindex.html index 2455529..3460d33 100644 --- a/docs/build/html/py-modindex.html +++ b/docs/build/html/py-modindex.html @@ -6,7 +6,7 @@ <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> - <title>Python Module Index — PubNub 3.7.1 documentation</title> + <title>Python Module Index — PubNub 3.7.2 documentation</title> <link rel="stylesheet" href="_static/default.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> @@ -14,7 +14,7 @@ <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: './', - VERSION: '3.7.1', + VERSION: '3.7.2', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true @@ -23,7 +23,7 @@ <script type="text/javascript" src="_static/jquery.js"></script> <script type="text/javascript" src="_static/underscore.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> - <link rel="top" title="PubNub 3.7.1 documentation" href="index.html" /> + <link rel="top" title="PubNub 3.7.2 documentation" href="index.html" /> <script type="text/javascript"> @@ -42,7 +42,7 @@ <li class="right" > <a href="#" title="Python Module Index" >modules</a> |</li> - <li><a href="index.html">PubNub 3.7.1 documentation</a> »</li> + <li><a href="index.html">PubNub 3.7.2 documentation</a> »</li> </ul> </div> @@ -101,7 +101,7 @@ <li class="right" > <a href="#" title="Python Module Index" >modules</a> |</li> - <li><a href="index.html">PubNub 3.7.1 documentation</a> »</li> + <li><a href="index.html">PubNub 3.7.2 documentation</a> »</li> </ul> </div> <div class="footer"> diff --git a/docs/build/html/search.html b/docs/build/html/search.html index a312b62..281f6ca 100644 --- a/docs/build/html/search.html +++ b/docs/build/html/search.html @@ -6,7 +6,7 @@ <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> - <title>Search — PubNub 3.7.1 documentation</title> + <title>Search — PubNub 3.7.2 documentation</title> <link rel="stylesheet" href="_static/default.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> @@ -14,7 +14,7 @@ <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: './', - VERSION: '3.7.1', + VERSION: '3.7.2', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true @@ -24,7 +24,7 @@ <script type="text/javascript" src="_static/underscore.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> <script type="text/javascript" src="_static/searchtools.js"></script> - <link rel="top" title="PubNub 3.7.1 documentation" href="index.html" /> + <link rel="top" title="PubNub 3.7.2 documentation" href="index.html" /> <script type="text/javascript"> jQuery(function() { Search.loadIndex("searchindex.js"); }); </script> @@ -43,7 +43,7 @@ <li class="right" > <a href="py-modindex.html" title="Python Module Index" >modules</a> |</li> - <li><a href="index.html">PubNub 3.7.1 documentation</a> »</li> + <li><a href="index.html">PubNub 3.7.2 documentation</a> »</li> </ul> </div> @@ -94,7 +94,7 @@ <li class="right" > <a href="py-modindex.html" title="Python Module Index" >modules</a> |</li> - <li><a href="index.html">PubNub 3.7.1 documentation</a> »</li> + <li><a href="index.html">PubNub 3.7.2 documentation</a> »</li> </ul> </div> <div class="footer"> diff --git a/docs/source/conf.py b/docs/source/conf.py index aa36d8e..e961ef0 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -54,9 +54,9 @@ copyright = u'2014, PubNub Inc.' # built documents. # # The short X.Y version. -version = '3.7.1' +version = '3.7.2' # The full version, including alpha/beta/rc tags. -release = '3.7.1' +release = '3.7.2' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. @@ -7,7 +7,7 @@ ## http://www.pubnub.com/ ## ----------------------------------- -## PubNub 3.7.1 Real-time Push Cloud API +## PubNub 3.7.2 Real-time Push Cloud API ## ----------------------------------- @@ -25,8 +25,6 @@ from base64 import urlsafe_b64encode from base64 import encodestring, decodestring import hmac from Crypto.Cipher import AES -from Crypto.Hash import MD5 - try: from hashlib import sha256 @@ -52,10 +50,9 @@ try: except ImportError: pass +#import urllib import socket -import sys import threading -from threading import current_thread try: import urllib3.HTTPConnection @@ -79,7 +76,21 @@ if sys.platform.startswith("linux"): ] elif sys.platform.startswith("darwin"): # From /usr/include/netinet/tcp.h - socket.TCP_KEEPALIVE = 0x10 # idle time used when SO_KEEPALIVE is enabled + + # idle time used when SO_KEEPALIVE is enabled + socket.TCP_KEEPALIVE = socket.TCP_KEEPALIVE \ + if hasattr(socket, 'TCP_KEEPALIVE') \ + else 0x10 + + # interval between keepalives + socket.TCP_KEEPINTVL = socket.TCP_KEEPINTVL \ + if hasattr(socket, 'TCP_KEEPINTVL') \ + else 0x101 + + # number of keepalives before close + socket.TCP_KEEPCNT = socket.TCP_KEEPCNT \ + if hasattr(socket, 'TCP_KEEPCNT') \ + else 0x102 default_socket_options += [ # Send first keepalive packet 200 seconds after last data packet @@ -137,7 +148,6 @@ except ImportError: ##### Twisted imports and globals ##### try: - from twisted.web.client import getPage from twisted.internet import reactor from twisted.internet.defer import Deferred from twisted.internet.protocol import Protocol @@ -146,11 +156,8 @@ try: from twisted.web.client import HTTPConnectionPool from twisted.web.http_headers import Headers from twisted.internet.ssl import ClientContextFactory - from twisted.internet.task import LoopingCall import twisted - from twisted.python.compat import ( - _PY3, unicode, intToBytes, networkString, nativeString) pnconn_pool = HTTPConnectionPool(reactor, persistent=True) pnconn_pool.maxPersistentPerHost = 100000 @@ -286,7 +293,7 @@ class PubnubBase(object): """ self.origin = origin - self.version = '3.7.1' + self.version = '3.7.2' self.limit = 1800 self.publish_key = publish_key self.subscribe_key = subscribe_key @@ -294,6 +301,7 @@ class PubnubBase(object): self.cipher_key = cipher_key self.ssl = ssl_on self.auth_key = auth_key + self.STATE = {} if self.ssl: self.origin = 'https://' + self.origin @@ -318,11 +326,12 @@ class PubnubBase(object): def _pam_sign(self, msg): - return urlsafe_b64encode(hmac.new( + sign = urlsafe_b64encode(hmac.new( self.secret_key.encode("utf-8"), msg.encode("utf-8"), sha256 ).digest()) + return quote(sign, safe="") def set_u(self, u=False): self.u = u @@ -658,8 +667,9 @@ class PubnubBase(object): if 'message' in response: callback_data['message'] = response['message'] - - callback(callback_data) + + if (callback is not None): + callback(callback_data) else: if (callback is not None): callback(response) @@ -751,7 +761,7 @@ class PubnubBase(object): callback=self._return_wrapped_callback(callback), error=self._return_wrapped_callback(error)) - def presence(self, channel, callback, error=None): + def presence(self, channel, callback, error=None, connect=None, disconnect=None, reconnect=None): """Subscribe to presence events on a channel. Only works in async mode @@ -767,9 +777,9 @@ class PubnubBase(object): Returns: None """ - return self.subscribe(channel+'-pnpres', callback=callback) + return self.subscribe(channel+'-pnpres', callback=callback, error=error, connect=connect, disconnect=disconnect, reconnect=reconnect) - def presence_group(self, channel_group, callback, error=None): + def presence_group(self, channel_group, callback, error=None, connect=None, disconnect=None, reconnect=None): """Subscribe to presence events on a channel group. Only works in async mode @@ -785,9 +795,9 @@ class PubnubBase(object): Returns: None """ - return self.subscribe_group(channel_group+'-pnpres', callback=callback) + return self.subscribe_group(channel_group+'-pnpres', callback=callback, error=error, connect=connect, disconnect=disconnect, reconnect=reconnect) - def here_now(self, channel, callback=None, error=None): + def here_now(self, channel, uuids=True, state=False, callback=None, error=None): """Get here now data. You can obtain information about the current state of a channel including @@ -844,15 +854,23 @@ class PubnubBase(object): urlcomponents.append('channel') urlcomponents.append(channel) + data = {'auth': self.auth_key, 'pnsdk' : self.pnsdk} + + if state is True: + data['state'] = '1' + + if uuids is False: + data['disable_uuids'] = '1' + ## Get Presence Here Now return self._request({"urlcomponents": urlcomponents, - 'urlparams': {'auth': self.auth_key, 'pnsdk' : self.pnsdk}}, + 'urlparams': data}, callback=self._return_wrapped_callback(callback), error=self._return_wrapped_callback(error)) def history(self, channel, count=100, reverse=False, - start=None, end=None, callback=None, error=None): + start=None, end=None, include_token=False, callback=None, error=None): """This method fetches historical messages of a channel. PubNub Storage/Playback Service provides real-time access to an unlimited @@ -900,17 +918,37 @@ class PubnubBase(object): [["Pub1","Pub2","Pub3","Pub4","Pub5"],13406746729185766,13406746845892666] """ + def _get_decrypted_history(resp): + try: + if resp is not None and isinstance(resp, (list)) and resp[1] is not None and self.cipher_key: + msgs = resp[0] + for i in range(0,len(msgs)): + msgs[i] = self.decrypt(msgs[i]) + except KeyError: + pass + return resp + + def _history_callback(resp): + if callback is not None: + callback(_get_decrypted_history(resp)) + + if callback is None: + history_cb = None + else: + history_cb = _history_callback + params = dict() params['count'] = count params['reverse'] = reverse params['start'] = start params['end'] = end - params['auth_key'] = self.auth_key + params['auth'] = self.auth_key params['pnsdk'] = self.pnsdk + params['include_token'] = 'true' if include_token else 'false' ## Get History - return self._request({'urlcomponents': [ + return _get_decrypted_history(self._request({'urlcomponents': [ 'v2', 'history', 'sub-key', @@ -918,8 +956,8 @@ class PubnubBase(object): 'channel', channel, ], 'urlparams': params}, - callback=self._return_wrapped_callback(callback), - error=self._return_wrapped_callback(error)) + callback=self._return_wrapped_callback(history_cb), + error=self._return_wrapped_callback(error))) def time(self, callback=None): """This function will return a 17 digit precision Unix epoch. @@ -965,7 +1003,7 @@ class PubnubBase(object): if ("urlparams" in request): url = url + '?' + "&".join([x + "=" + str(y) for x, y in request[ "urlparams"].items() if y is not None and len(str(y)) > 0]) - + #print(url) return url def _channel_registry(self, url=None, params=None, callback=None, error=None): @@ -1503,7 +1541,7 @@ class PubnubCoreAsync(PubnubBase): for i in l: func(i) - def subscribe(self, channels, callback, error=None, + def subscribe(self, channels, callback, state=None, error=None, connect=None, disconnect=None, reconnect=None, sync=False): """Subscribe to data on a channel. @@ -1522,6 +1560,9 @@ class PubnubCoreAsync(PubnubBase): callback: (function) This callback is called on receiving a message from the channel. + state: (dict) + State to be set. + error: (function) (optional) This callback is called on an error event @@ -1538,8 +1579,8 @@ class PubnubCoreAsync(PubnubBase): None """ - return self._subscribe(channels=channels, callback=callback, error=error, - connect=connect, disconnect=disconnect, reconnect=reconnect, sync=sync) + return self._subscribe(channels=channels, callback=callback, state=state, error=error, + connect=connect, disconnect=disconnect, reconnect=reconnect) def subscribe_group(self, channel_groups, callback, error=None, connect=None, disconnect=None, reconnect=None, sync=False): @@ -1577,20 +1618,16 @@ class PubnubCoreAsync(PubnubBase): """ return self._subscribe(channel_groups=channel_groups, callback=callback, error=error, - connect=connect, disconnect=disconnect, reconnect=reconnect, sync=sync) + connect=connect, disconnect=disconnect, reconnect=reconnect) - def _subscribe(self, channels=None, channel_groups=None, callback=None, error=None, - connect=None, disconnect=None, reconnect=None, sync=False): + def _subscribe(self, channels=None, channel_groups=None, state=None, callback=None, error=None, + connect=None, disconnect=None, reconnect=None): with self._tt_lock: self.last_timetoken = self.timetoken if self.timetoken != 0 \ else self.last_timetoken self.timetoken = 0 - if sync is True and self.subscribe_sync is not None: - self.subscribe_sync(args) - return - def _invoke(func, msg=None, channel=None, real_channel=None): if func is not None: if msg is not None and channel is not None and real_channel is not None: @@ -1656,11 +1693,17 @@ class PubnubCoreAsync(PubnubBase): if channel_list is None: for ch in self.subscriptions: chobj = self.subscriptions[ch] - _invoke(chobj['error'], error) + try: + _invoke(chobj['error'], error, ch) + except TypeError: + _invoke(chobj['error'], error) else: for ch in channel_list: chobj = self.subscriptions[ch] - _invoke(chobj['error'], error) + try: + _invoke(chobj['error'], error, ch) + except TypeError: + _invoke(chobj['error'], error) def _get_channel(): for ch in self.subscriptions: @@ -1689,6 +1732,11 @@ class PubnubCoreAsync(PubnubBase): 'reconnect': reconnect, 'error': error } + if state is not None: + if channel in self.STATE: + self.STATE[channel] = state[channel] + else: + self.STATE[channel] = state if channel_groups is not None: channel_groups = channel_groups if isinstance( @@ -1767,7 +1815,7 @@ class PubnubCoreAsync(PubnubBase): if ch[1] in self.subscription_groups or ch[1] in self.subscriptions: try: chobj = self.subscription_groups[ch[1]] - except KeyError as k: + except KeyError: chobj = self.subscriptions[ch[1]] _invoke(chobj['callback'], self.decrypt(response_list[ch[0]]), @@ -1800,6 +1848,15 @@ class PubnubCoreAsync(PubnubBase): if len(channel_list) <= 0: channel_list = ',' + data = {"uuid": self.uuid, "auth": self.auth_key, + 'pnsdk' : self.pnsdk, 'channel-group' : channel_group_list} + + + st = json.dumps(self.STATE) + + if len(st) > 2: + data['state'] = quote(st,safe="") + ## CONNECT TO PUBNUB SUBSCRIBE SERVERS #try: self.SUB_RECEIVER = self._request({"urlcomponents": [ @@ -1808,8 +1865,7 @@ class PubnubCoreAsync(PubnubBase): channel_list, '0', str(self.timetoken) - ], "urlparams": {"uuid": self.uuid, "auth": self.auth_key, - 'pnsdk' : self.pnsdk, 'channel-group' : channel_group_list}}, + ], "urlparams": data}, sub_callback, error_callback, single=True, timeout=320) @@ -1852,6 +1908,10 @@ class PubnubCoreAsync(PubnubBase): self.subscriptions[channel]['timetoken'] = 0 self.subscriptions[channel]['first'] = False self.leave_channel(channel=channel) + + # remove channel from STATE + self.STATE.pop(channel, None) + self.CONNECT() def unsubscribe_group(self, channel_group): @@ -1909,62 +1969,6 @@ class PubnubCore(PubnubCoreAsync): self.timetoken = 0 self.accept_encoding = 'gzip' - def subscribe_sync(self, channel, callback, timetoken=0): - """ - #** - #* Subscribe - #* - #* This is BLOCKING. - #* Listen for a message on a channel. - #* - #* @param array args with channel and callback. - #* @return false on fail, array on success. - #** - - ## Subscribe Example - def receive(message) : - print(message) - return True - - pubnub.subscribe({ - 'channel' : 'hello_world', - 'callback' : receive - }) - - """ - - subscribe_key = self.subscribe_key - - ## Begin Subscribe - while True: - try: - ## Wait for Message - response = self._request({"urlcomponents": [ - 'subscribe', - subscribe_key, - channel, - '0', - str(timetoken) - ], "urlparams": {"uuid": self.uuid, 'pnsdk' : self.pnsdk}}) - - messages = response[0] - timetoken = response[1] - - ## If it was a timeout - if not len(messages): - continue - - ## Run user Callback and Reconnect if user permits. - for message in messages: - if not callback(self.decrypt(message)): - return - - except Exception: - time.sleep(1) - - return True - - class HTTPClient: def __init__(self, pubnub, url, urllib_func=None, callback=None, error=None, id=None, timeout=5): @@ -2006,7 +2010,7 @@ class HTTPClient: self.pubnub.latest_sub_callback['id'] = 0 try: data = json.loads(data) - except ValueError as e: + except ValueError: _invoke(self.pubnub.latest_sub_callback['error'], {'error': 'json decoding error'}) return @@ -2062,6 +2066,8 @@ def _requests_request(url, timeout=5): except requests.exceptions.Timeout as error: msg = str(error) return (json.dumps(msg), 0) + #print (resp.text) + #print (resp.status_code) return (resp.text, resp.status_code) @@ -2184,7 +2190,7 @@ class Pubnub(PubnubCore): if callback is None: return get_data_for_user(self._request_sync(request, timeout=timeout)) else: - self._request_async(request, callback, error, single=single, timeout=timeout) + return self._request_async(request, callback, error, single=single, timeout=timeout) # Pubnub Twisted @@ -2243,7 +2249,7 @@ class PubnubTwisted(PubnubCoreAsync): try: request = agent.request( 'GET', url, Headers(self.headers), None) - except TypeError as te: + except TypeError: request = agent.request( 'GET', url.encode(), Headers(self.headers), None) @@ -2270,10 +2276,10 @@ class PubnubTwisted(PubnubCoreAsync): return None try: data = json.loads(data) - except ValueError as e: + except ValueError: try: data = json.loads(data.decode("utf-8")) - except ValueError as e: + except ValueError: _invoke(error, {'error': 'json decode error'}) if 'error' in data and 'status' in data and 'status' != 200: @@ -2368,10 +2374,10 @@ class PubnubTornado(PubnubCoreAsync): try: data = json.loads(body) - except TypeError as e: + except TypeError: try: data = json.loads(body.decode("utf-8")) - except ValueError as ve: + except ValueError: _invoke(error, {'error': 'json decode error'}) if 'error' in data and 'status' in data and 'status' != 200: diff --git a/python-tornado/README.md b/python-tornado/README.md index 59e79b7..eaa2fe0 100644 --- a/python-tornado/README.md +++ b/python-tornado/README.md @@ -12,7 +12,7 @@ pubnub.start() #### Import ``` -from Pubnub import PubnubTornado as Pubnub +from pubnub import PubnubTornado as Pubnub ``` #### Init diff --git a/python-tornado/examples/audit.py b/python-tornado/examples/audit.py index 77d189b..3f6af13 100644 --- a/python-tornado/examples/audit.py +++ b/python-tornado/examples/audit.py @@ -7,7 +7,7 @@ import sys -from Pubnub import PubnubTornado as Pubnub +from pubnub import PubnubTornado as 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-tornado/examples/grant.py b/python-tornado/examples/grant.py index daf7434..e2726b9 100644 --- a/python-tornado/examples/grant.py +++ b/python-tornado/examples/grant.py @@ -7,7 +7,7 @@ import sys -from Pubnub import PubnubTornado as Pubnub +from pubnub import PubnubTornado as 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-tornado/examples/here-now.py b/python-tornado/examples/here-now.py index 5c195f1..7018be7 100644 --- a/python-tornado/examples/here-now.py +++ b/python-tornado/examples/here-now.py @@ -7,7 +7,7 @@ import sys -from Pubnub import PubnubTornado as Pubnub +from pubnub import PubnubTornado 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' diff --git a/python-tornado/examples/history.py b/python-tornado/examples/history.py index daf1c6e..ff85545 100644 --- a/python-tornado/examples/history.py +++ b/python-tornado/examples/history.py @@ -7,7 +7,7 @@ import sys -from Pubnub import PubnubTornado as Pubnub +from pubnub import PubnubTornado 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' diff --git a/python-tornado/examples/presence_group.py b/python-tornado/examples/presence_group.py index bb89420..38181cf 100644 --- a/python-tornado/examples/presence_group.py +++ b/python-tornado/examples/presence_group.py @@ -7,7 +7,7 @@ import sys -from Pubnub import PubnubTornado as Pubnub +from pubnub import PubnubTornado 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' diff --git a/python-tornado/examples/publish.py b/python-tornado/examples/publish.py index 04e88fd..997adfa 100644 --- a/python-tornado/examples/publish.py +++ b/python-tornado/examples/publish.py @@ -7,7 +7,7 @@ import sys -from Pubnub import PubnubTornado as Pubnub +from pubnub import PubnubTornado 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' diff --git a/python-tornado/examples/revoke.py b/python-tornado/examples/revoke.py index 1e4487e..4bdd9c7 100644 --- a/python-tornado/examples/revoke.py +++ b/python-tornado/examples/revoke.py @@ -7,7 +7,7 @@ import sys -from Pubnub import PubnubTornado as Pubnub +from pubnub import PubnubTornado as 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-tornado/examples/subscribe.py b/python-tornado/examples/subscribe.py index 72f0fc1..96c6ebb 100644 --- a/python-tornado/examples/subscribe.py +++ b/python-tornado/examples/subscribe.py @@ -7,7 +7,7 @@ import sys -from Pubnub import PubnubTornado as Pubnub +from pubnub import PubnubTornado 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' diff --git a/python-tornado/examples/subscribe_group.py b/python-tornado/examples/subscribe_group.py index eddcf8d..394821a 100644 --- a/python-tornado/examples/subscribe_group.py +++ b/python-tornado/examples/subscribe_group.py @@ -7,7 +7,7 @@ import sys -from Pubnub import PubnubTornado as Pubnub +from pubnub import PubnubTornado 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' diff --git a/python-tornado/migration.md b/python-tornado/migration.md index d5fd8f0..6a1abf3 100644 --- a/python-tornado/migration.md +++ b/python-tornado/migration.md @@ -7,10 +7,10 @@ ``` # Pre 3.5: -from Pubnub import Pubnub +from pubnub import Pubnub # New in 3.5+ -from Pubnub import PubnubTornado as Pubnub +from pubnub import PubnubTornado as Pubnub ``` diff --git a/python-tornado/tests/benchmark.py b/python-tornado/tests/benchmark.py index 748fe3b..6023017 100644 --- a/python-tornado/tests/benchmark.py +++ b/python-tornado/tests/benchmark.py @@ -12,7 +12,7 @@ import sys import datetime import tornado -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted 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' diff --git a/python-tornado/tests/delivery.py b/python-tornado/tests/delivery.py index 0181403..21486da 100644 --- a/python-tornado/tests/delivery.py +++ b/python-tornado/tests/delivery.py @@ -14,7 +14,7 @@ import datetime import time import math -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted as Pubnub ## ----------------------------------------------------------------------- ## Configuration diff --git a/python-tornado/tests/subscribe-test.py b/python-tornado/tests/subscribe-test.py index bcbbc7e..7b57415 100755 --- a/python-tornado/tests/subscribe-test.py +++ b/python-tornado/tests/subscribe-test.py @@ -11,7 +11,7 @@ import sys import datetime -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted as Pubnub from functools import partial from threading import current_thread import threading diff --git a/python-tornado/tests/test_grant_async.py b/python-tornado/tests/test_grant_async.py index b51b275..c03cf08 100644 --- a/python-tornado/tests/test_grant_async.py +++ b/python-tornado/tests/test_grant_async.py @@ -1,6 +1,6 @@ -from Pubnub import PubnubTornado as Pubnub +from pubnub import PubnubTornado as Pubnub import time pubnub = Pubnub("demo","demo") diff --git a/python-tornado/tests/test_publish_async.py b/python-tornado/tests/test_publish_async.py index 391297d..1f442ed 100644 --- a/python-tornado/tests/test_publish_async.py +++ b/python-tornado/tests/test_publish_async.py @@ -1,6 +1,6 @@ -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted as Pubnub import time pubnub = Pubnub("demo","demo") diff --git a/python-twisted/README.md b/python-twisted/README.md index c74fb0d..0677eff 100644 --- a/python-twisted/README.md +++ b/python-twisted/README.md @@ -12,7 +12,7 @@ pubnub.start() #### Import ``` -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted as Pubnub ``` #### Init diff --git a/python-twisted/examples/audit.py b/python-twisted/examples/audit.py index b99a8a9..d5cd34b 100644 --- a/python-twisted/examples/audit.py +++ b/python-twisted/examples/audit.py @@ -7,7 +7,7 @@ import sys -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted as 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-twisted/examples/echo-client.py b/python-twisted/examples/echo-client.py index 6f6d1c7..f329d47 100644 --- a/python-twisted/examples/echo-client.py +++ b/python-twisted/examples/echo-client.py @@ -8,7 +8,7 @@ ## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ## Import Libs ## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted as Pubnub ## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ## Configuration diff --git a/python-twisted/examples/echo-server.py b/python-twisted/examples/echo-server.py index 65f9c57..06ee81a 100644 --- a/python-twisted/examples/echo-server.py +++ b/python-twisted/examples/echo-server.py @@ -8,7 +8,7 @@ ## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ## Import Libs ## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted as Pubnub ## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ## Configuration diff --git a/python-twisted/examples/grant.py b/python-twisted/examples/grant.py index 053c1f9..3d6284f 100644 --- a/python-twisted/examples/grant.py +++ b/python-twisted/examples/grant.py @@ -7,7 +7,7 @@ import sys -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted as 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-twisted/examples/here-now.py b/python-twisted/examples/here-now.py index 38b79f8..acf8cb0 100644 --- a/python-twisted/examples/here-now.py +++ b/python-twisted/examples/here-now.py @@ -7,7 +7,7 @@ import sys -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted 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' diff --git a/python-twisted/examples/history.py b/python-twisted/examples/history.py index 81974ec..d8c4063 100644 --- a/python-twisted/examples/history.py +++ b/python-twisted/examples/history.py @@ -7,7 +7,7 @@ import sys -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted 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' diff --git a/python-twisted/examples/publish.py b/python-twisted/examples/publish.py index 13b5357..a687c59 100644 --- a/python-twisted/examples/publish.py +++ b/python-twisted/examples/publish.py @@ -7,7 +7,7 @@ import sys -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted 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' diff --git a/python-twisted/examples/revoke.py b/python-twisted/examples/revoke.py index fab0497..3b87015 100644 --- a/python-twisted/examples/revoke.py +++ b/python-twisted/examples/revoke.py @@ -7,7 +7,7 @@ import sys -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted as 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-twisted/examples/subscribe.py b/python-twisted/examples/subscribe.py index 9c73439..1704f7c 100644 --- a/python-twisted/examples/subscribe.py +++ b/python-twisted/examples/subscribe.py @@ -7,7 +7,7 @@ import sys -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted 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' diff --git a/python-twisted/examples/subscribe_group.py b/python-twisted/examples/subscribe_group.py index 67dbac5..2bfd40a 100644 --- a/python-twisted/examples/subscribe_group.py +++ b/python-twisted/examples/subscribe_group.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-twisted/migration.md b/python-twisted/migration.md index 49de5a9..924fa7c 100644 --- a/python-twisted/migration.md +++ b/python-twisted/migration.md @@ -7,10 +7,10 @@ ``` # Pre 3.5: -from Pubnub import Pubnub +from pubnub import Pubnub # New in 3.5+ -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted as Pubnub ``` diff --git a/python-twisted/tests/benchmark.py b/python-twisted/tests/benchmark.py index b6477c0..1d5f914 100644 --- a/python-twisted/tests/benchmark.py +++ b/python-twisted/tests/benchmark.py @@ -12,7 +12,7 @@ import sys import datetime from twisted.internet import reactor -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted 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' diff --git a/python-twisted/tests/delivery.py b/python-twisted/tests/delivery.py index 30ce55f..46690f8 100644 --- a/python-twisted/tests/delivery.py +++ b/python-twisted/tests/delivery.py @@ -14,7 +14,7 @@ import datetime import time import math -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted as Pubnub ## ----------------------------------------------------------------------- ## Configuration diff --git a/python-twisted/tests/subscribe-test.py b/python-twisted/tests/subscribe-test.py index ba74992..2f4c208 100755 --- a/python-twisted/tests/subscribe-test.py +++ b/python-twisted/tests/subscribe-test.py @@ -11,7 +11,7 @@ import sys import datetime -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted as Pubnub from functools import partial from threading import current_thread import threading diff --git a/python-twisted/tests/test_grant_async.py b/python-twisted/tests/test_grant_async.py index 5b33b11..0f79570 100644 --- a/python-twisted/tests/test_grant_async.py +++ b/python-twisted/tests/test_grant_async.py @@ -1,6 +1,6 @@ -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted as Pubnub import time pubnub = Pubnub("demo","demo") diff --git a/python-twisted/tests/test_publish_async.py b/python-twisted/tests/test_publish_async.py index 391297d..1f442ed 100644 --- a/python-twisted/tests/test_publish_async.py +++ b/python-twisted/tests/test_publish_async.py @@ -1,6 +1,6 @@ -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted as Pubnub import time pubnub = Pubnub("demo","demo") diff --git a/python-twisted/tests/unit-test-full.py b/python-twisted/tests/unit-test-full.py index 3aecf12..2335ef6 100644 --- a/python-twisted/tests/unit-test-full.py +++ b/python-twisted/tests/unit-test-full.py @@ -18,7 +18,7 @@ ## ----------------------------------- import sys -from Pubnub import PubnubTwisted as Pubnub +from pubnub import PubnubTwisted 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' diff --git a/python/README.md b/python/README.md index 80a6b08..10bb30f 100644 --- a/python/README.md +++ b/python/README.md @@ -191,6 +191,23 @@ def callback(message): pubnub.history(channel=channel, count=2, callback=callback, error=callback) ``` +#### HISTORY (including timetokens) + +``` +# 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) +``` + + #### GRANT ``` 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' diff --git a/python/tests/subscribe-test.py b/python/tests/subscribe-test.py deleted file mode 100755 index a1b1826..0000000 --- a/python/tests/subscribe-test.py +++ /dev/null @@ -1,154 +0,0 @@ -## www.pubnub.com - PubNub Real-time push service in the cloud. -# coding=utf8 - -## PubNub Real-time Push APIs and Notifications Framework -## Copyright (c) 2010 Stephen Blum -## http://www.pubnub.com/ - -## ----------------------------------- -## PubNub 3.1 Real-time Push Cloud API -## ----------------------------------- - -import sys -import datetime -from Pubnub import PubnubAsync as Pubnub -from functools import partial -from threading import current_thread -import threading -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 None -ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False - -## ----------------------------------------------------------------------- -## Initiate Pubnub State -## ----------------------------------------------------------------------- -#pubnub = Pubnub( publish_key, subscribe_key, secret_key, cipher_key, ssl_on ) -pubnub = Pubnub(publish_key, subscribe_key, secret_key, ssl_on) -crazy = 'hello_world' - -current = -1 - -errors = 0 -received = 0 - -## ----------------------------------------------------------------------- -## Subscribe Example -## ----------------------------------------------------------------------- - - -def message_received(message): - print(message) - - -def check_received(message): - global current - global errors - global received - print(message) - print(current) - if message <= current: - print('ERROR') - #sys.exit() - errors += 1 - else: - received += 1 - print('active thread count : ' + str(threading.activeCount())) - print('errors = ' + str(errors)) - print(current_thread().getName() + ' , ' + 'received = ' + str(received)) - - if received != message: - print('********** MISSED **************** ' + str(message - received)) - current = message - - -def connected_test(ch): - print('Connected ' + ch) - - -def connected(ch): - pass - - -''' -pubnub.subscribe({ - 'channel' : 'abcd1', - 'connect' : connected, - 'callback' : message_received -}) -''' - - -def cb1(): - pubnub.subscribe({ - 'channel': 'efgh1', - 'connect': connected, - 'callback': message_received - }) - - -def cb2(): - pubnub.subscribe({ - 'channel': 'dsm-test', - 'connect': connected_test, - 'callback': check_received - }) - - -def cb3(): - pubnub.unsubscribe({'channel': 'efgh1'}) - - -def cb4(): - pubnub.unsubscribe({'channel': 'abcd1'}) - - -def subscribe(channel): - pubnub.subscribe({ - 'channel': channel, - 'connect': connected, - 'callback': message_received - }) - - -pubnub.timeout(15, cb1) - -pubnub.timeout(30, cb2) - - -pubnub.timeout(45, cb3) - -pubnub.timeout(60, cb4) - -#''' -for x in range(1, 1000): - #print x - def y(t): - subscribe('channel-' + str(t)) - - def z(t): - pubnub.unsubscribe({'channel': 'channel-' + str(t)}) - - pubnub.timeout(x + 5, partial(y, x)) - pubnub.timeout(x + 25, partial(z, x)) - x += 10 -#''' - -''' -for x in range(1,1000): - def cb(r): print r , ' : ', threading.activeCount() - def y(t): - pubnub.publish({ - 'message' : t, - 'callback' : cb, - 'channel' : 'dsm-test' - }) - - - pubnub.timeout(x + 1, partial(y,x)) - x += 1 -''' - - -pubnub.start() diff --git a/python/tests/test_cg.py b/python/tests/test_cg.py index a823e44..f4a1080 100644 --- a/python/tests/test_cg.py +++ b/python/tests/test_cg.py @@ -1,4 +1,4 @@ -from Pubnub import Pubnub +from pubnub import Pubnub import time import random diff --git a/python/tests/test_grant.py b/python/tests/test_grant.py index 9ecafdf..57f0b14 100644 --- a/python/tests/test_grant.py +++ b/python/tests/test_grant.py @@ -1,6 +1,6 @@ -from Pubnub import Pubnub +from pubnub import Pubnub import time pubnub = Pubnub("demo","demo") diff --git a/python/tests/test_grant_async.py b/python/tests/test_grant_async.py deleted file mode 100644 index 4d38a0a..0000000 --- a/python/tests/test_grant_async.py +++ /dev/null @@ -1,369 +0,0 @@ - - -from Pubnub import Pubnub -import time - -pubnub = Pubnub("demo","demo") -pubnub_pam = Pubnub("pub-c-c077418d-f83c-4860-b213-2f6c77bde29a", - "sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe", "sec-c-OGU3Y2Q4ZWUtNDQwMC00NTI1LThjNWYtNWJmY2M4OGIwNjEy") - - - -# Grant permission read true, write true, on channel ( Async Mode ) -def test_1(): - resp = {'response' : None} - def _callback(response): - resp['response'] = response - - def _error(response): - resp['response'] = response - - pubnub_pam.grant(channel="abcd", auth_key="abcd", read=True, write=True, ttl=1, callback=_callback, error=_error) - time.sleep(2) - assert resp['response'] == { - 'message': u'Success', - 'payload': {u'auths': {u'abcd': {u'r': 1, u'w': 1}}, - u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', - u'level': u'user', u'channel': u'abcd', u'ttl': 1} - } - - -# Grant permission read false, write false, on channel ( Async Mode ) -def test_2(): - resp = {'response' : None} - def _callback(response): - resp['response'] = response - - def _error(response): - resp['response'] = response - - pubnub_pam.grant(channel="abcd", auth_key="abcd", read=False, write=False, ttl=1, callback=_callback, error=_error) - time.sleep(2) - assert resp['response'] == { - 'message': u'Success', - 'payload': {u'auths': {u'abcd': {u'r': 0, u'w': 0}}, - u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', - u'level': u'user', u'channel': u'abcd', u'ttl': 1} - } - - -# Grant permission read True, write false, on channel ( Async Mode ) -def test_3(): - resp = {'response' : None} - def _callback(response): - resp['response'] = response - - def _error(response): - resp['response'] = response - - pubnub_pam.grant(channel="abcd", auth_key="abcd", read=True, write=False, ttl=1, callback=_callback, error=_error) - time.sleep(2) - assert resp['response'] == { - 'message': u'Success', - 'payload': {u'auths': {u'abcd': {u'r': 1, u'w': 0}}, - u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', - u'level': u'user', u'channel': u'abcd', u'ttl': 1} - } - -# Grant permission read False, write True, on channel ( Async Mode ) -def test_4(): - resp = {'response' : None} - def _callback(response): - resp['response'] = response - - def _error(response): - resp['response'] = response - - pubnub_pam.grant(channel="abcd", auth_key="abcd", read=False, write=True, ttl=1, callback=_callback, error=_error) - time.sleep(2) - assert resp['response'] == { - 'message': u'Success', - 'payload': {u'auths': {u'abcd': {u'r': 0, u'w': 1}}, - u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', - u'level': u'user', u'channel': u'abcd', u'ttl': 1} - } - - -# Grant permission read False, write True, on channel ( Async Mode ), TTL 10 -def test_5(): - resp = {'response' : None} - def _callback(response): - resp['response'] = response - - def _error(response): - resp['response'] = response - - pubnub_pam.grant(channel="abcd", auth_key="abcd", read=False, write=True, ttl=10, callback=_callback, error=_error) - time.sleep(2) - assert resp['response'] == { - 'message': u'Success', - 'payload': {u'auths': {u'abcd': {u'r': 0, u'w': 1}}, - u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', - u'level': u'user', u'channel': u'abcd', u'ttl': 10} - } - - -# Grant permission read False, write True, without channel ( Async Mode ), TTL 10 -def test_6(): - resp = {'response' : None} - def _callback(response): - resp['response'] = response - - def _error(response): - resp['response'] = response - - pubnub_pam.grant(auth_key="abcd", read=False, write=True, ttl=10, callback=_callback, error=_error) - time.sleep(2) - assert resp['response'] == { - 'message': u'Success', - 'payload': { u'r': 0, u'w': 1, - u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', - u'level': u'subkey', u'ttl': 10} - } - - -# Grant permission read False, write False, without channel ( Async Mode ) -def test_7(): - resp = {'response' : None} - def _callback(response): - resp['response'] = response - - def _error(response): - resp['response'] = response - - pubnub_pam.grant(auth_key="abcd", read=False, write=False, callback=_callback, error=_error) - time.sleep(2) - assert resp['response'] == { - 'message': u'Success', - 'payload': { u'r': 0, u'w': 0, - u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', - u'level': u'subkey', u'ttl': 1} - } - - -# Complete flow , try publish on forbidden channel, grant permission to subkey and try again. ( Sync Mode) - -def test_8(): - channel = "test_8-" + str(time.time()) - message = "Hello World" - auth_key = "auth-" + channel - pubnub_pam.set_auth_key(auth_key) - - - - def _cb1(resp, ch=None): - assert False - def _err1(resp): - assert resp['message'] == 'Forbidden' - assert resp['payload'] == {u'channels': [channel]} - pubnub_pam.publish(channel=channel,message=message, callback=_cb1, error=_err1) - time.sleep(2) - - - def _cb2(resp, ch=None): - assert resp == { - 'message': u'Success', - 'payload': {u'auths': {auth_key : {u'r': 1, u'w': 1}}, - u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', - u'level': u'user', u'channel': channel, u'ttl': 10} - } - def _err2(resp): - assert False - - - resp = pubnub_pam.grant(channel=channel, read=True, write=True, auth_key=auth_key, ttl=10, callback=_cb2, error=_err2) - time.sleep(2) - - def _cb3(resp, ch=None): - assert resp[0] == 1 - def _err3(resp): - assert False - - pubnub_pam.publish(channel=channel,message=message, callback=_cb3, error=_err3) - time.sleep(2) - - - - - -# Complete flow , try publish on forbidden channel, grant permission to authkey and try again. -# then revoke and try again -def test_9(): - channel = "test_9-" + str(time.time()) - message = "Hello World" - auth_key = "auth-" + channel - pubnub_pam.set_auth_key(auth_key) - - def _cb1(resp, ch=None): - assert False - def _err1(resp): - assert resp['message'] == 'Forbidden' - assert resp['payload'] == {u'channels': [channel]} - pubnub_pam.publish(channel=channel,message=message, callback=_cb1, error=_err1) - time.sleep(2) - - - def _cb2(resp, ch=None): - assert resp == { - 'message': u'Success', - 'payload': {u'auths': {auth_key : {u'r': 1, u'w': 1}}, - u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', - u'level': u'user', u'channel': channel, u'ttl': 10} - } - def _err2(resp): - assert False - - - pubnub_pam.grant(channel=channel, read=True, write=True, auth_key=auth_key, ttl=10, callback=_cb2, error=_err2) - time.sleep(2) - - def _cb3(resp, ch=None): - assert resp[0] == 1 - def _err3(resp): - assert False - - pubnub_pam.publish(channel=channel,message=message, callback=_cb3, error=_err3) - time.sleep(2) - - def _cb4(resp, ch=None): - assert resp == { - 'message': u'Success', - 'payload': {u'auths': {auth_key : {u'r': 0, u'w': 0}}, - u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', - u'level': u'user', u'channel': channel, u'ttl': 1} - } - def _err4(resp): - assert False - pubnub_pam.revoke(channel=channel, auth_key=auth_key, callback=_cb4, error=_err4) - time.sleep(2) - - def _cb5(resp, ch=None): - assert False - def _err5(resp): - assert resp['message'] == 'Forbidden' - assert resp['payload'] == {u'channels': [channel]} - pubnub_pam.publish(channel=channel,message=message, callback=_cb5, error=_err5) - time.sleep(2) - - - - -# Complete flow , try publish on forbidden channel, grant permission channel level for subkey and try again. -# then revoke and try again -def test_10(): - channel = "test_10-" + str(time.time()) - message = "Hello World" - auth_key = "auth-" + channel - pubnub_pam.set_auth_key(auth_key) - - def _cb1(resp, ch=None): - assert False - def _err1(resp): - assert resp['message'] == 'Forbidden' - assert resp['payload'] == {u'channels': [channel]} - pubnub_pam.publish(channel=channel,message=message, callback=_cb1, error=_err1) - time.sleep(2) - - - def _cb2(resp, ch=None): - assert resp == { - 'message': u'Success', - 'payload': { u'channels': {channel: {u'r': 1, u'w': 1}}, - u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', - u'level': u'channel', u'ttl': 10} - } - def _err2(resp): - assert False - - - pubnub_pam.grant(channel=channel, read=True, write=True, ttl=10, callback=_cb2, error=_err2) - time.sleep(2) - - def _cb3(resp, ch=None): - assert resp[0] == 1 - def _err3(resp): - assert False - - pubnub_pam.publish(channel=channel,message=message, callback=_cb3, error=_err3) - time.sleep(2) - - def _cb4(resp, ch=None): - assert resp == { - 'message': u'Success', - 'payload': { u'channels': {channel : {u'r': 0, u'w': 0}}, - u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', - u'level': u'channel', u'ttl': 1} - } - def _err4(resp): - assert False - pubnub_pam.revoke(channel=channel, callback=_cb4, error=_err4) - time.sleep(2) - - def _cb5(resp, ch=None): - assert False - def _err5(resp): - assert resp['message'] == 'Forbidden' - assert resp['payload'] == {u'channels': [channel]} - pubnub_pam.publish(channel=channel,message=message, callback=_cb5, error=_err5) - time.sleep(2) - - - -# Complete flow , try publish on forbidden channel, grant permission subkey level for subkey and try again. -# then revoke and try again -def test_11(): - channel = "test_11-" + str(time.time()) - message = "Hello World" - auth_key = "auth-" + channel - pubnub_pam.set_auth_key(auth_key) - - def _cb1(resp, ch=None): - assert False - def _err1(resp): - assert resp['message'] == 'Forbidden' - assert resp['payload'] == {u'channels': [channel]} - pubnub_pam.publish(channel=channel,message=message, callback=_cb1, error=_err1) - time.sleep(2) - - - def _cb2(resp, ch=None): - assert resp == { - 'message': u'Success', - 'payload': { u'r': 1, u'w': 1, - u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', - u'level': u'subkey', u'ttl': 10} - } - def _err2(resp): - assert False - - - pubnub_pam.grant(read=True, write=True, ttl=10, callback=_cb2, error=_err2) - time.sleep(2) - - def _cb3(resp, ch=None): - assert resp[0] == 1 - def _err3(resp): - assert False - - pubnub_pam.publish(channel=channel,message=message, callback=_cb3, error=_err3) - time.sleep(2) - - def _cb4(resp, ch=None): - assert resp == { - 'message': u'Success', - 'payload': {u'r': 0, u'w': 0, - u'subscribe_key': u'sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe', - u'level': u'subkey', u'ttl': 1} - } - def _err4(resp): - assert False - pubnub_pam.revoke(callback=_cb4, error=_err4) - time.sleep(2) - - def _cb5(resp, ch=None): - assert False - def _err5(resp): - assert resp['message'] == 'Forbidden' - assert resp['payload'] == {u'channels': [channel]} - pubnub_pam.publish(channel=channel,message=message, callback=_cb5, error=_err5) - time.sleep(2) diff --git a/python/tests/test_history.py b/python/tests/test_history.py new file mode 100644 index 0000000..a5feb37 --- /dev/null +++ b/python/tests/test_history.py @@ -0,0 +1,58 @@ +from pubnub import Pubnub +import time +import random +from nose.tools import with_setup + + +pubnub = Pubnub("ds","ds") +pubnub_enc = Pubnub(publish_key="ds",subscribe_key="ds",cipher_key="enigma") +pubnub_pam = Pubnub(publish_key="pam", subscribe_key="pam", secret_key="pam") + + +def rand(msg): + return "rand-" + str(random.random()) + "-" + msg + +channel = rand("channel") +channel_enc = rand("channel_enc") +channel_pam = rand("channel_pam") + +messages = [] + + +def setup_func(): + for i in range(0,20): + msg = rand("message-" + str(i)) + messages.append(msg) + pubnub_pam.grant(channel=channel_pam, read=True, write=True, ttl=144000) + pubnub.publish(channel=channel, message=msg) + pubnub_enc.publish(channel=channel_enc, message=msg) + pubnub_pam.publish(channel=channel_pam, message=msg) + + +@with_setup(setup_func) +def test_1(): + time.sleep(3) + hresp = pubnub.history(channel=channel, count=20) + hresp2 = pubnub_enc.history(channel=channel_enc, count=20) + hresp3 = pubnub_pam.history(channel=channel_pam, count=20) + hresp4 = pubnub_pam.history(channel=channel_pam + "no_rw", count=20) + assert hresp[0] == messages + assert hresp2[0] == messages + assert hresp3[0] == messages + assert hresp4['message'] == 'Forbidden' + assert channel_pam + "no_rw" in hresp4['payload']['channels'] + +def test_2(): + time.sleep(3) + hresp = pubnub.history(channel=channel, count=20, include_token=True) + hresp2 = pubnub_enc.history(channel=channel_enc, count=20, include_token=True) + hresp3 = pubnub_pam.history(channel=channel_pam, count=20, include_token=True) + hresp4 = pubnub_pam.history(channel=channel_pam + "no_rw", count=20, include_token=True) + assert len(hresp[0]) == len(messages) + assert hresp[0][0]['timetoken'] + assert len(hresp2[0]) == len(messages) + assert hresp2[0][0]['timetoken'] + assert len(hresp3[0]) == len(messages) + assert hresp3[0][0]['timetoken'] + assert hresp4['message'] == 'Forbidden' + assert channel_pam + "no_rw" in hresp4['payload']['channels'] diff --git a/python/tests/test_publish_async.py b/python/tests/test_publish_async.py deleted file mode 100644 index 7270727..0000000 --- a/python/tests/test_publish_async.py +++ /dev/null @@ -1,228 +0,0 @@ - - -from Pubnub import Pubnub -import time - -pubnub = Pubnub("demo","demo") -pubnub_enc = Pubnub("demo", "demo", cipher_key="enigma") -pubnub_pam = Pubnub("pub-c-c077418d-f83c-4860-b213-2f6c77bde29a", - "sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe", "sec-c-OGU3Y2Q4ZWUtNDQwMC00NTI1LThjNWYtNWJmY2M4OGIwNjEy") - - - -# Publish and receive string -def test_1(): - - channel = "test_1-" + str(time.time()) - message = "I am a string" - - def _cb(resp, ch=None): - assert resp == message - pubnub.unsubscribe(channel) - - def _connect(resp): - pubnub.publish(channel,message) - - def _error(resp): - assert False - - pubnub.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive array -def test_2(): - - channel = "test_2-" + str(time.time()) - message = [1,2] - - def _cb(resp, ch=None): - assert resp == message - pubnub.unsubscribe(channel) - - def _connect(resp): - pubnub.publish(channel,message) - - def _error(resp): - assert False - - pubnub.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive json object -def test_3(): - - channel = "test_2-" + str(time.time()) - message = { "a" : "b" } - - def _cb(resp, ch=None): - assert resp == message - pubnub.unsubscribe(channel) - - def _connect(resp): - pubnub.publish(channel,message) - - def _error(resp): - assert False - - pubnub.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive number -def test_4(): - - channel = "test_2-" + str(time.time()) - message = 100 - - def _cb(resp, ch=None): - assert resp == message - pubnub.unsubscribe(channel) - - def _connect(resp): - pubnub.publish(channel,message) - - def _error(resp): - assert False - - pubnub.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive number string -def test_5(): - - channel = "test_5-" + str(time.time()) - message = "100" - - def _cb(resp, ch=None): - assert resp == message - pubnub.unsubscribe(channel) - - def _connect(resp): - pubnub.publish(channel,message) - - def _error(resp): - assert False - - pubnub.subscribe(channel, callback=_cb, connect=_connect, error=_error) - - -# Publish and receive string (Encryption enabled) -def test_6(): - - channel = "test_6-" + str(time.time()) - message = "I am a string" - - def _cb(resp, ch=None): - assert resp == message - pubnub_enc.unsubscribe(channel) - - def _connect(resp): - pubnub_enc.publish(channel,message) - - def _error(resp): - assert False - - pubnub_enc.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive array (Encryption enabled) -def test_7(): - - channel = "test_7-" + str(time.time()) - message = [1,2] - - def _cb(resp, ch=None): - assert resp == message - pubnub_enc.unsubscribe(channel) - - def _connect(resp): - pubnub.publish(channel,message) - - def _error(resp): - assert False - - pubnub.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive json object (Encryption enabled) -def test_8(): - - channel = "test_8-" + str(time.time()) - message = { "a" : "b" } - - def _cb(resp, ch=None): - assert resp == message - pubnub_enc.unsubscribe(channel) - - def _connect(resp): - pubnub_enc.publish(channel,message) - - def _error(resp): - assert False - - pubnub_enc.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive number (Encryption enabled) -def test_9(): - - channel = "test_9-" + str(time.time()) - message = 100 - - def _cb(resp, ch=None): - assert resp == message - pubnub_enc.unsubscribe(channel) - - def _connect(resp): - pubnub_enc.publish(channel,message) - - def _error(resp): - assert False - - pubnub_enc.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive number string (Encryption enabled) -def test_10(): - - channel = "test_10-" + str(time.time()) - message = "100" - - def _cb(resp, ch=None): - assert resp == message - pubnub_enc.unsubscribe(channel) - - def _connect(resp): - pubnub_enc.publish(channel,message) - - def _error(resp): - assert False - - pubnub_enc.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive object string (Encryption enabled) -def test_11(): - - channel = "test_11-" + str(time.time()) - message = '{"a" : "b"}' - - def _cb(resp, ch=None): - assert resp == message - pubnub_enc.unsubscribe(channel) - - def _connect(resp): - pubnub_enc.publish(channel,message) - - def _error(resp): - assert False - - pubnub_enc.subscribe(channel, callback=_cb, connect=_connect, error=_error) - -# Publish and receive array string (Encryption enabled) -def test_12(): - - channel = "test_12-" + str(time.time()) - message = '[1,2]' - - def _cb(resp, ch=None): - assert resp == message - pubnub_enc.unsubscribe(channel) - - def _connect(resp): - pubnub_enc.publish(channel,message) - - def _error(resp): - assert False - - pubnub_enc.subscribe(channel, callback=_cb, connect=_connect, error=_error) @@ -2,12 +2,12 @@ from setuptools import setup, find_packages setup( name='pubnub', - version='3.7.1', + version='3.7.2', description='PubNub Real-time push service in the cloud', author='Stephen Blum', author_email='support@pubnub.com', url='http://pubnub.com', - py_modules=['Pubnub'], + py_modules=['pubnub'], license='MIT', classifiers=( 'Development Status :: 5 - Production/Stable', |
