11import os
22import sys
33import time
4- import httplib
4+ try :
5+ import http .client as httplib
6+ except ImportError :
7+ import httplib
58import hmac
69import json
710import hashlib
8- import urllib
11+ try :
12+ from urllib .parse import quote
13+ except ImportError :
14+ from urllib import quote
915import re
1016import socket
1117
18+ if sys .version < '3' :
19+ text_type = unicode
20+ else :
21+ text_type = str
22+
1223host = 'api.pusherapp.com'
1324port = 80
1425app_id = None
@@ -52,7 +63,7 @@ def __init__(self, app_id=None, key=None, secret=None, host=None, port=None, enc
5263 self ._channels = {}
5364
5465 def __getitem__ (self , key ):
55- if not self ._channels . has_key ( key ) :
66+ if key not in self ._channels :
5667 return self ._make_channel (key )
5768 return self ._channels [key ]
5869
@@ -66,7 +77,7 @@ def __init__(self, name, pusher):
6677 self .name = str (name )
6778 if not channel_name_re .match (self .name ):
6879 raise NameError ("Invalid channel id: %s" % self .name )
69- self .path = '/apps/%s/channels/%s/events' % (self .pusher .app_id , urllib . quote (self .name ))
80+ self .path = '/apps/%s/channels/%s/events' % (self .pusher .app_id , quote (self .name ))
7081
7182 def trigger (self , event , data = {}, socket_id = None , timeout = socket ._GLOBAL_DEFAULT_TIMEOUT ):
7283 json_data = json .dumps (data , cls = self .pusher .encoder )
@@ -87,25 +98,25 @@ def trigger(self, event, data={}, socket_id=None, timeout=socket._GLOBAL_DEFAULT
8798 def signed_query (self , event , json_data , socket_id ):
8899 query_string = self .compose_querystring (event , json_data , socket_id )
89100 string_to_sign = "POST\n %s\n %s" % (self .path , query_string )
90- signature = hmac .new (self .pusher .secret , string_to_sign , hashlib .sha256 ).hexdigest ()
101+ signature = hmac .new (self .pusher .secret . encode ( 'utf-8' ) , string_to_sign . encode ( 'utf-8' ) , hashlib .sha256 ).hexdigest ()
91102 return "%s&auth_signature=%s" % (query_string , signature )
92103
93104 def compose_querystring (self , event , json_data , socket_id ):
94105 hasher = hashlib .md5 ()
95- hasher .update (json_data )
106+ hasher .update (json_data . encode ( 'UTF-8' ) )
96107 hash_str = hasher .hexdigest ()
97108 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 )
98109 if socket_id :
99- ret += "&socket_id=" + unicode (socket_id )
110+ ret += "&socket_id=" + text_type (socket_id )
100111 return ret
101112
102113 def send_request (self , signed_path , data_string , timeout = socket ._GLOBAL_DEFAULT_TIMEOUT ):
103114 if self .pusher .port == 443 :
104- http = httplib .HTTPSConnection (self .pusher .host , self .pusher .port , timeout = timeout )
115+ client = httplib .HTTPConnection (self .pusher .host , self .pusher .port , timeout = timeout )
105116 else :
106- http = httplib .HTTPConnection (self .pusher .host , self .pusher .port , timeout = timeout )
107- http .request ('POST' , signed_path , data_string , {'Content-Type' : 'application/json' })
108- resp = http .getresponse ()
117+ client = httplib .HTTPSConnection (self .pusher .host , self .pusher .port , timeout = timeout )
118+ client .request ('POST' , signed_path , data_string , {'Content-Type' : 'application/json' })
119+ resp = client .getresponse ()
109120 return resp .status , resp .read ()
110121
111122 def authenticate (self , socket_id , custom_data = None ):
0 commit comments