aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDevendra2014-03-25 10:50:39 +0530
committerDevendra2014-03-25 10:50:39 +0530
commitb8d1dd86a0d8c4261d4f3765f3ca227d7b555c84 (patch)
treed5cd2e092f44ff36e739fd029afc7b028c8c087f
parent80edcffbfe140a6d19c65deca24e1ba1c0f49b99 (diff)
downloadpubnub-python-b8d1dd86a0d8c4261d4f3765f3ca227d7b555c84.tar.bz2
fixing encryption, and changing from urllib3 to urllib
-rw-r--r--common/PubnubCrypto.py9
-rw-r--r--python/Pubnub.py21
-rwxr-xr-xpython/examples/publish-example.py32
-rw-r--r--python/unassembled/Platform.py12
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):