diff options
| author | Devendra | 2015-04-28 22:37:30 +0530 | 
|---|---|---|
| committer | Devendra | 2015-04-28 22:37:30 +0530 | 
| commit | 58511aeb2796142cabf0c2f4740db03daf9c0fa3 (patch) | |
| tree | 1dc22080d28979bef3e0e7f9d5420d9973aea73b | |
| parent | 3aa150b81bf7f0159086ce0ec61b9171fdeb670a (diff) | |
| download | pubnub-python-58511aeb2796142cabf0c2f4740db03daf9c0fa3.tar.bz2 | |
publish with POST
| -rw-r--r-- | Pubnub.py | 65 | ||||
| -rw-r--r-- | python/examples/publish_post.py | 49 | 
2 files changed, 90 insertions, 24 deletions
| @@ -709,7 +709,7 @@ class PubnubBase(object):              error=self._return_wrapped_callback(error)) -    def publish(self, channel, message, callback=None, error=None): +    def publish(self, channel, message,callback=None, error=None, post=False):          """Publishes data on a channel.          The publish() method is used to send a message to all subscribers of a channel. @@ -753,17 +753,29 @@ class PubnubBase(object):          message = self.encrypt(message)          ## Send Message -        return self._request({"urlcomponents": [ -            'publish', -            self.publish_key, -            self.subscribe_key, -            '0', -            channel, -            '0', -            message -        ], 'urlparams': {'auth': self.auth_key, 'pnsdk' : self.pnsdk}}, -            callback=self._return_wrapped_callback(callback), -            error=self._return_wrapped_callback(error)) +        if post is False: +            return self._request({"urlcomponents": [ +                'publish', +                self.publish_key, +                self.subscribe_key, +                '0', +                channel, +                '0', +                message +            ], 'urlparams': {'auth': self.auth_key, 'pnsdk' : self.pnsdk}}, +                callback=self._return_wrapped_callback(callback), +                error=self._return_wrapped_callback(error)) +        else: +            return self._request({"urlcomponents": [ +                'publish', +                self.publish_key, +                self.subscribe_key, +                '0', +                channel, +                '0' +            ], 'urlparams': {'auth': self.auth_key, 'pnsdk' : self.pnsdk}}, +                callback=self._return_wrapped_callback(callback), +                error=self._return_wrapped_callback(error), json_post_data=message)                  def presence(self, channel, callback, error=None):          """Subscribe to presence events on a channel. @@ -1981,7 +1993,7 @@ class PubnubCore(PubnubCoreAsync):  class HTTPClient:      def __init__(self, pubnub, url, urllib_func=None, -                 callback=None, error=None, id=None, timeout=5): +                 callback=None, error=None, id=None, timeout=5, json_post_data=None, headers=None):          self.url = url          self.id = id          self.callback = callback @@ -1990,6 +2002,8 @@ class HTTPClient:          self._urllib_func = urllib_func          self.timeout = timeout          self.pubnub = pubnub +        self.json_post_data = json_post_data +        self.headers = headers      def cancel(self):          self.stop = True @@ -2005,7 +2019,7 @@ class HTTPClient:          if self._urllib_func is None:              return -        resp = self._urllib_func(self.url, timeout=self.timeout) +        resp = self._urllib_func(self.url, timeout=self.timeout, json_post_data=self.json_post_data, headers=self.headers)          data = resp[0]          code = resp[1] @@ -2065,9 +2079,12 @@ s = requests.Session()  #s.mount('https://pubsub.pubnub.com', HTTPAdapter(max_retries=1)) -def _requests_request(url, timeout=5): +def _requests_request(url, timeout=5, json_post_data=None, headers=None):      try: -        resp = s.get(url, timeout=timeout) +        if json_post_data is None: +            resp = s.get(url, timeout=timeout, headers=headers) +        else: +            resp = s.post(url, data=json_post_data, headers=headers)      except requests.exceptions.HTTPError as http_error:          resp = http_error      except requests.exceptions.ConnectionError as error: @@ -2150,21 +2167,21 @@ class Pubnub(PubnubCore):          thread.daemon = self.daemon          thread.start() -    def _request_async(self, request, callback=None, error=None, single=False, timeout=5): +    def _request_async(self, request, callback=None, error=None, single=False, timeout=5, json_post_data=None):          global _urllib_request          ## Build URL          url = self.getUrl(request)          if single is True:              id = time.time()              client = HTTPClient(self, url=url, urllib_func=_urllib_request, -                                callback=None, error=None, id=id, timeout=timeout) +                                callback=None, error=None, id=id, timeout=timeout, json_post_data=json_post_data, headers={'user-agent' : self.pnsdk})              with self.latest_sub_callback_lock:                  self.latest_sub_callback['id'] = id                  self.latest_sub_callback['callback'] = callback                  self.latest_sub_callback['error'] = error          else:              client = HTTPClient(self, url=url, urllib_func=_urllib_request, -                                callback=callback, error=error, timeout=timeout) +                                callback=callback, error=error, timeout=timeout, json_post_data=json_post_data, headers={'user-agent' : self.pnsdk})          thread = threading.Thread(target=client.run)          thread.daemon = self.daemon @@ -2174,12 +2191,12 @@ class Pubnub(PubnubCore):              client.cancel()          return abort -    def _request_sync(self, request, timeout=5): +    def _request_sync(self, request, timeout=5, json_post_data=None):          global _urllib_request          ## Build URL          url = self.getUrl(request)          ## Send Request Expecting JSONP Response -        response = _urllib_request(url, timeout=timeout) +        response = _urllib_request(url, timeout=timeout, json_post_data=json_post_data, headers={'user-agent' : self.pnsdk})          try:              resp_json = json.loads(response[0])          except ValueError: @@ -2194,11 +2211,11 @@ class Pubnub(PubnubCore):          return resp_json -    def _request(self, request, callback=None, error=None, single=False, timeout=5): +    def _request(self, request, callback=None, error=None, single=False, timeout=5, json_post_data=None):          if callback is None: -            return get_data_for_user(self._request_sync(request, timeout=timeout)) +            return get_data_for_user(self._request_sync(request, timeout=timeout, json_post_data=json_post_data))          else: -            self._request_async(request, callback, error, single=single, timeout=timeout) +            self._request_async(request, callback, error, single=single, timeout=timeout, json_post_data=json_post_data)  # Pubnub Twisted diff --git a/python/examples/publish_post.py b/python/examples/publish_post.py new file mode 100644 index 0000000..99bef99 --- /dev/null +++ b/python/examples/publish_post.py @@ -0,0 +1,49 @@ +## 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/ + + +import sys +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 'demo' +cipher_key = len(sys.argv) > 4 and sys.argv[4] or '' +ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False + +## ----------------------------------------------------------------------- +## Initiate Pubnub State +## ----------------------------------------------------------------------- +pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key, +                secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on) +channel = 'hello_world' +message = 'Hello World !!!' + + + +# Synchronous usage +print pubnub.publish(channel, message, post=True) + +# Asynchronous usage + + +def callback(message): +    print(message) + +pubnub.publish(channel, message, callback=callback, error=callback, post=True) + + + +print pubnub.publish(channel, 1, post=True) +print pubnub.publish(channel, "1", post=True) +print pubnub.publish(channel, "abcd", post=True) +print pubnub.publish(channel, 0, post=True) +print pubnub.publish(channel, "0", post=True) +print pubnub.publish(channel, "True", post=True) +print pubnub.publish(channel, "False", post=True) +print pubnub.publish(channel, [1,2], post=True) +print pubnub.publish(channel, {"a" : 1}, post=True) | 
