Skip to content

Commit 7a81ba1

Browse files
author
mbrrg
committed
Python 3 support
1 parent 94dcdd3 commit 7a81ba1

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

pusher/__init__.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
import os
22
import sys
33
import time
4-
import httplib
4+
try:
5+
import http.client as httplib
6+
except ImportError:
7+
import httplib
58
import hmac
69
import json
710
import hashlib
8-
import urllib
11+
try:
12+
from urllib.parse import quote
13+
except ImportError:
14+
from urllib import quote
915
import re
1016
import socket
1117

18+
if sys.version < '3':
19+
text_type = unicode
20+
else:
21+
text_type = str
22+
1223
host = 'api.pusherapp.com'
1324
port = 80
1425
app_id = None
@@ -45,7 +56,7 @@ def __init__(self, app_id=None, key=None, secret=None, host=None, port=None, enc
4556
self._channels = {}
4657

4758
def __getitem__(self, key):
48-
if not self._channels.has_key(key):
59+
if key not in self._channels:
4960
return self._make_channel(key)
5061
return self._channels[key]
5162

@@ -59,7 +70,7 @@ def __init__(self, name, pusher):
5970
self.name = str(name)
6071
if not channel_name_re.match(self.name):
6172
raise NameError("Invalid channel id: %s" % self.name)
62-
self.path = '/apps/%s/channels/%s/events' % (self.pusher.app_id, urllib.quote(self.name))
73+
self.path = '/apps/%s/channels/%s/events' % (self.pusher.app_id, quote(self.name))
6374

6475
def trigger(self, event, data={}, socket_id=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
6576
json_data = json.dumps(data, cls=self.pusher.encoder)
@@ -80,22 +91,22 @@ def trigger(self, event, data={}, socket_id=None, timeout=socket._GLOBAL_DEFAULT
8091
def signed_query(self, event, json_data, socket_id):
8192
query_string = self.compose_querystring(event, json_data, socket_id)
8293
string_to_sign = "POST\n%s\n%s" % (self.path, query_string)
83-
signature = hmac.new(self.pusher.secret, string_to_sign, hashlib.sha256).hexdigest()
94+
signature = hmac.new(self.pusher.secret.encode('utf-8'), string_to_sign.encode('utf-8'), hashlib.sha256).hexdigest()
8495
return "%s&auth_signature=%s" % (query_string, signature)
8596

8697
def compose_querystring(self, event, json_data, socket_id):
8798
hasher = hashlib.md5()
88-
hasher.update(json_data)
99+
hasher.update(json_data.encode('UTF-8'))
89100
hash_str = hasher.hexdigest()
90101
ret = "auth_key=%s&auth_timestamp=%s&auth_version=1.0&body_md5=%s&name=%s" % (self.pusher.key, int(time.time()), hash_str, event)
91102
if socket_id:
92-
ret += "&socket_id=" + unicode(socket_id)
103+
ret += "&socket_id=" + text_type(socket_id)
93104
return ret
94105

95106
def send_request(self, signed_path, data_string, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
96-
http = httplib.HTTPConnection(self.pusher.host, self.pusher.port, timeout=timeout)
97-
http.request('POST', signed_path, data_string, {'Content-Type': 'application/json'})
98-
resp = http.getresponse()
107+
client = httplib.HTTPConnection(self.pusher.host, self.pusher.port, timeout=timeout)
108+
client.request('POST', signed_path, data_string, {'Content-Type': 'application/json'})
109+
resp = client.getresponse()
99110
return resp.status, resp.read()
100111

101112
def authenticate(self, socket_id, custom_data=None):

pusher/acceptance_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import unittest, re, httplib, time, cgi
1+
import unittest
22
from nose.tools import *
33

44
import sys
@@ -32,4 +32,4 @@ def test_trigger_with_data_value_containing_percent(self):
3232
channel = my_pusher['test-channel']
3333
result = channel.trigger('test-event', {'message': "fish %"})
3434

35-
eq_(result, True)
35+
eq_(result, True)

0 commit comments

Comments
 (0)