diff options
| -rw-r--r-- | common/PubnubCrypto.py | 9 | ||||
| -rw-r--r-- | python/Pubnub.py | 21 | ||||
| -rwxr-xr-x | python/examples/publish-example.py | 32 | ||||
| -rw-r--r-- | python/unassembled/Platform.py | 12 | 
4 files changed, 49 insertions, 25 deletions
| diff --git a/common/PubnubCrypto.py b/common/PubnubCrypto.py index df7cb8d..1f5fc8d 100644 --- a/common/PubnubCrypto.py +++ b/common/PubnubCrypto.py @@ -28,7 +28,7 @@ class PubnubCrypto() :          #**          """          padding = block_size - (len(msg) % block_size) -        return msg + chr(padding)*padding +        return msg + (chr(padding)*padding).encode('utf-8')      def depad( self, msg ):          """ @@ -50,7 +50,7 @@ class PubnubCrypto() :          #* @return key in MD5 format          #**          """ -        return hashlib.sha256(key).hexdigest() +        return hashlib.sha256(key.encode("utf-8")).hexdigest()      def encrypt( self, key, msg ):          """ @@ -64,8 +64,7 @@ class PubnubCrypto() :          secret = self.getSecret(key)          Initial16bytes='0123456789012345'          cipher = AES.new(secret[0:32],AES.MODE_CBC,Initial16bytes) -        enc = encodestring(cipher.encrypt(self.pad(msg))) -        return enc +        return encodestring(cipher.encrypt(self.pad(msg.encode('utf-8')))).decode('utf-8')      def decrypt( self, key, msg ):          """          #** @@ -78,4 +77,4 @@ class PubnubCrypto() :          secret = self.getSecret(key)          Initial16bytes='0123456789012345'          cipher = AES.new(secret[0:32],AES.MODE_CBC,Initial16bytes) -        return self.depad((cipher.decrypt(decodestring(msg)))) +        return (cipher.decrypt(decodestring(msg.encode('utf-8')))).decode('utf-8') diff --git a/python/Pubnub.py b/python/Pubnub.py index 5dd4c7d..aa7068b 100644 --- a/python/Pubnub.py +++ b/python/Pubnub.py @@ -40,7 +40,7 @@ class PubnubCrypto() :          #**          """          padding = block_size - (len(msg) % block_size) -        return msg + chr(padding)*padding +        return msg + (chr(padding)*padding).encode('utf-8')      def depad( self, msg ):          """ @@ -62,7 +62,7 @@ class PubnubCrypto() :          #* @return key in MD5 format          #**          """ -        return hashlib.sha256(key).hexdigest() +        return hashlib.sha256(key.encode("utf-8")).hexdigest()      def encrypt( self, key, msg ):          """ @@ -76,8 +76,7 @@ class PubnubCrypto() :          secret = self.getSecret(key)          Initial16bytes='0123456789012345'          cipher = AES.new(secret[0:32],AES.MODE_CBC,Initial16bytes) -        enc = encodestring(cipher.encrypt(self.pad(msg))) -        return enc +        return encodestring(cipher.encrypt(self.pad(msg.encode('utf-8')))).decode('utf-8')      def decrypt( self, key, msg ):          """          #** @@ -90,7 +89,7 @@ class PubnubCrypto() :          secret = self.getSecret(key)          Initial16bytes='0123456789012345'          cipher = AES.new(secret[0:32],AES.MODE_CBC,Initial16bytes) -        return self.depad((cipher.decrypt(decodestring(msg)))) +        return (cipher.decrypt(decodestring(msg.encode('utf-8')))).decode('utf-8')  try: import json @@ -575,7 +574,8 @@ class PubnubCore(PubnubBase):          return True -import urllib3 + +import urllib.request  class Pubnub(PubnubCore):      def __init__( @@ -597,17 +597,16 @@ class Pubnub(PubnubCore):              origin = origin,              uuid = pres_uuid          ) -        self.http = urllib3.PoolManager(timeout=310)           def _request( self, request, callback = None ) :          ## Build URL          url = self.getUrl(request) - +        print(url)          ## Send Request Expecting JSONP Response          try: -            response = self.http.request('GET', url) -            resp_json = json.loads(response.data.decode("utf-8")) -        except: +            response = urllib.request.urlopen(url,timeout=310) +            resp_json = json.loads(response.read().decode("utf-8")) +        except Exception as e:              return None          if (callback): diff --git a/python/examples/publish-example.py b/python/examples/publish-example.py index 55f19ea..31ae198 100755 --- a/python/examples/publish-example.py +++ b/python/examples/publish-example.py @@ -4,14 +4,40 @@ sys.path.append('..')  from Pubnub import Pubnub  ## Initiate Class -pubnub = Pubnub( publish_key='demo', subscribe_key='demo', ssl_on=False ) +pubnub = Pubnub( publish_key='demo', subscribe_key='demo', cipher_key='enigma', ssl_on=False ) +#pubnub = Pubnub( publish_key='demo', subscribe_key='demo', ssl_on=False )  ## Publish Example  info = pubnub.publish({ -    'channel' : 'hello_world', +    'channel' : 'abcd',      'message' : { -        'some_text' : 'Hello my World' +        'iam' : 'object'      }  })  print(info) +info = pubnub.publish({ +    'channel' : 'abcd', +    'message' : "hi I am string" +}) +print(info) + +info = pubnub.publish({ +    'channel' : 'abcd', +    'message' : 1234 +}) +print(info) + +info = pubnub.publish({ +    'channel' : 'abcd', +    'message' : "1234" +}) +print(info) + +info = pubnub.publish({ +    'channel' : 'abcd', +    'message' : [ +        'i' , 'am', 'array' +    ] +}) +print(info) diff --git a/python/unassembled/Platform.py b/python/unassembled/Platform.py index 627a350..55ec449 100644 --- a/python/unassembled/Platform.py +++ b/python/unassembled/Platform.py @@ -1,4 +1,5 @@ -import urllib3 + +import urllib.request  class Pubnub(PubnubCore):      def __init__( @@ -20,17 +21,16 @@ class Pubnub(PubnubCore):              origin = origin,              uuid = pres_uuid          ) -        self.http = urllib3.PoolManager(timeout=310)           def _request( self, request, callback = None ) :          ## Build URL          url = self.getUrl(request) - +        print(url)          ## Send Request Expecting JSONP Response          try: -            response = self.http.request('GET', url) -            resp_json = json.loads(response.data.decode("utf-8")) -        except: +            response = urllib.request.urlopen(url,timeout=310) +            resp_json = json.loads(response.read().decode("utf-8")) +        except Exception as e:              return None          if (callback): | 
