diff --git a/.gitignore b/.gitignore index ff20996..02fbb33 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.pyc /*.egg-info/ /dist/ +.idea \ No newline at end of file diff --git a/pusherclient/__init__.py b/pusherclient/__init__.py index acd5ec9..98a38fa 100755 --- a/pusherclient/__init__.py +++ b/pusherclient/__init__.py @@ -3,6 +3,7 @@ import hashlib import hmac import logging +import sys try: @@ -10,6 +11,7 @@ except ImportError: import json +PY3 = sys.version_info[0] == 3 VERSION = "0.2.0" @@ -21,6 +23,12 @@ class Pusher(object): def __init__(self, key, secure=True, secret=None, user_data=None, log_level=logging.INFO, daemon=True, port=None, reconnect_interval=10): self.key = key + if PY3: + string_type = str + else: + string_type = basestring + if isinstance(secret, string_type): + secret = secret.encode('utf8') self.secret = secret self.user_data = user_data or {} @@ -106,7 +114,7 @@ def _generate_private_key(socket_id, key, channel_name, secret): if socket_id and key and channel_name and secret: subject = "%s:%s" % (socket_id, channel_name) - h = hmac.new(secret, subject, hashlib.sha256) + h = hmac.new(secret, subject.encode('utf8'), hashlib.sha256) auth_key = "%s:%s" % (key, h.hexdigest()) return auth_key @@ -117,7 +125,7 @@ def _generate_presence_key(socket_id, key, channel_name, secret, user_data): if socket_id and key and channel_name and secret and user_data: subject = "%s:%s:%s" % (socket_id, channel_name, json.dumps(user_data)) - h = hmac.new(secret, subject, hashlib.sha256) + h = hmac.new(secret, subject.encode('utf8'), hashlib.sha256) auth_key = "%s:%s" % (key, h.hexdigest()) return auth_key diff --git a/pusherclient/connection.py b/pusherclient/connection.py index 75fe53f..9a2a970 100755 --- a/pusherclient/connection.py +++ b/pusherclient/connection.py @@ -2,6 +2,7 @@ import websocket import logging import time +import ssl try: import simplejson as json @@ -105,7 +106,7 @@ def _connect(self): on_close=self._on_close ) - self.socket.run_forever() + self.socket.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE}) while self.needs_reconnect and not self.disconnect_called: self.logger.info("Attempting to connect again in %s seconds." @@ -116,7 +117,7 @@ def _connect(self): # We need to set this flag since closing the socket will set it to # false self.socket.keep_running = True - self.socket.run_forever() + self.socket.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE}) def _on_open(self, ws): self.logger.info("Connection: Connection opened")