diff options
| -rw-r--r-- | Pubnub.py | 6 | ||||
| -rw-r--r-- | python-twisted/examples/echo-client.py | 88 | ||||
| -rw-r--r-- | python-twisted/examples/echo-server.py | 93 | 
3 files changed, 184 insertions, 3 deletions
| @@ -371,7 +371,7 @@ class PubnubBase(object):          self.auth_key = auth_key      def get_auth_key(self): -        return auth_key +        return self.auth_key      def grant(self, channel=None, channel_group=None, auth_key=False, read=False,                write=False, manage=False, ttl=5, callback=None, error=None): @@ -943,7 +943,7 @@ class PubnubBase(object):          if (channels is not None):              if (type(channels) is list): -                channels = channels.join(',') +                channels = ','.join(channels)              params[mode] = channels              #params['cloak'] = 'true' if CLOAK is True else 'false'          else: @@ -1781,7 +1781,7 @@ class PubnubTwisted(PubnubCoreAsync):          self.headers['V'] = [self.version]          self.pnsdk = 'PubNub-Python-' + 'Twisted' + '/' + self.version -    def _request(self, request, callback=None, error=None, single=False): +    def _request(self, request, callback=None, error=None, single=False, timeout=5):          global pnconn_pool          def _invoke(func, data): diff --git a/python-twisted/examples/echo-client.py b/python-twisted/examples/echo-client.py new file mode 100644 index 0000000..6f6d1c7 --- /dev/null +++ b/python-twisted/examples/echo-client.py @@ -0,0 +1,88 @@ +## www.pubnub.com - PubNub - Data Stream Network +# coding=utf8 + +## PubNub Real-time Push APIs and Notifications Framework +## Copyright (c) 2010 Stephen Blum +## http://www.pubnub.com/ + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Import Libs +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +from Pubnub import PubnubTwisted as Pubnub + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Configuration +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +publish_key    = 'pub-c-f6a20151-db8d-45af-ba42-def0edaa459f' +subscribe_key  = 'sub-c-b5ff3208-7f64-11e4-b601-02ee2ddab7fe' +secret_key     = 'demo' +server_channel = 'echo-server' +client_channel = 'echo-channel' + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Create PubNub Instance +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +pubnub = Pubnub( +    publish_key=publish_key, +    subscribe_key=subscribe_key, +    secret_key=secret_key, +    ssl_on=True +) + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Error Log +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +def error_log(data): print(data) + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Access Log +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +def access_log(data): print(data) + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Respond +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +def request(): +    pubnub.publish( +        server_channel, +        { 'response' : client_channel, 'body' : "Hello" }, +        callback=access_log, +        error=error_log +    ) + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Request Received +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +def onResponse( data, channel ): +    print("Channel: %s | Req: %s" % (channel,data)) +    request() + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Ready to Receive Requests +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +def onReady(message): +    print("Ready to Receive Requests on '%s'" % server_channel) +    request() + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Network Recovered +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +def onReconnect(message): print("RECONNECTED") + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Network Failed +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +def onDisconnect(message): print("DISCONNECTED") + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Start Echo Server +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +pubnub.subscribe( +    client_channel, +    callback=onResponse, +    error=error_log, +    connect=onReady, +    reconnect=onReconnect, +    disconnect=onDisconnect +) +pubnub.start() diff --git a/python-twisted/examples/echo-server.py b/python-twisted/examples/echo-server.py new file mode 100644 index 0000000..65f9c57 --- /dev/null +++ b/python-twisted/examples/echo-server.py @@ -0,0 +1,93 @@ +## www.pubnub.com - PubNub - Data Stream Network +# coding=utf8 + +## PubNub Real-time Push APIs and Notifications Framework +## Copyright (c) 2010 Stephen Blum +## http://www.pubnub.com/ + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Import Libs +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +from Pubnub import PubnubTwisted as Pubnub + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Configuration +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +publish_key    = 'pub-c-f6a20151-db8d-45af-ba42-def0edaa459f' +subscribe_key  = 'sub-c-b5ff3208-7f64-11e4-b601-02ee2ddab7fe' +secret_key     = 'demo' +server_channel = 'echo-server' + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Create PubNub Instance +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +pubnub = Pubnub( +    publish_key=publish_key, +    subscribe_key=subscribe_key, +    secret_key=secret_key, +    ssl_on=True +) + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Error Log +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +def error_log(data): print(data) + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Access Log +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +def access_log(data): print(data) + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Respond +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +def respond( channel, body ): +    pubnub.publish( +        channel, +        body, +        callback=access_log, +        error=error_log +    ) + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Request Received +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +def onRequest( request, channel ): +    response_channel = request['response'] +    response_body    = request['body'] + +    print("Channel: %s | Req: %s" % (channel,request)) + +    respond( +        channel=response_channel, +        body=response_body +    ) + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Ready to Receive Requests +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +def onReady(message): +    print("Ready to Receive Requests on '%s'" % server_channel) + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Network Recovered +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +def onReconnect(message): print("RECONNECTED") + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Network Failed +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +def onDisconnect(message): print("DISCONNECTED") + +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +## Start Echo Server +## =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +pubnub.subscribe( +    server_channel, +    callback=onRequest, +    error=error_log, +    connect=onReady, +    reconnect=onReconnect, +    disconnect=onDisconnect +) +pubnub.start() | 
