1
1
import os
2
2
import sys
3
3
import time
4
- import httplib
4
+ try :
5
+ import http .client as httplib
6
+ except ImportError :
7
+ import httplib
5
8
import hmac
6
9
import json
7
10
import hashlib
8
- import urllib
11
+ try :
12
+ from urllib .parse import quote
13
+ except ImportError :
14
+ from urllib import quote
9
15
import re
10
16
import socket
11
17
18
+ if sys .version < '3' :
19
+ text_type = unicode
20
+ else :
21
+ text_type = str
22
+
12
23
host = 'api.pusherapp.com'
13
24
port = 80
14
25
app_id = None
@@ -49,7 +60,7 @@ def __init__(self, app_id=None, key=None, secret=None, host=None, port=None, enc
49
60
self ._channels = {}
50
61
51
62
def __getitem__ (self , key ):
52
- if not self ._channels . has_key ( key ) :
63
+ if key not in self ._channels :
53
64
return self ._make_channel (key )
54
65
return self ._channels [key ]
55
66
@@ -63,7 +74,7 @@ def __init__(self, name, pusher):
63
74
self .name = str (name )
64
75
if not channel_name_re .match (self .name ):
65
76
raise NameError ("Invalid channel id: %s" % self .name )
66
- self .path = '/apps/%s/channels/%s/events' % (self .pusher .app_id , urllib . quote (self .name ))
77
+ self .path = '/apps/%s/channels/%s/events' % (self .pusher .app_id , quote (self .name ))
67
78
68
79
def trigger (self , event , data = {}, socket_id = None , timeout = socket ._GLOBAL_DEFAULT_TIMEOUT ):
69
80
json_data = json .dumps (data , cls = self .pusher .encoder )
@@ -84,22 +95,22 @@ def trigger(self, event, data={}, socket_id=None, timeout=socket._GLOBAL_DEFAULT
84
95
def signed_query (self , event , json_data , socket_id ):
85
96
query_string = self .compose_querystring (event , json_data , socket_id )
86
97
string_to_sign = "POST\n %s\n %s" % (self .path , query_string )
87
- signature = hmac .new (self .pusher .secret , string_to_sign , hashlib .sha256 ).hexdigest ()
98
+ signature = hmac .new (self .pusher .secret . encode ( 'utf-8' ) , string_to_sign . encode ( 'utf-8' ) , hashlib .sha256 ).hexdigest ()
88
99
return "%s&auth_signature=%s" % (query_string , signature )
89
100
90
101
def compose_querystring (self , event , json_data , socket_id ):
91
102
hasher = hashlib .md5 ()
92
- hasher .update (json_data )
103
+ hasher .update (json_data . encode ( 'UTF-8' ) )
93
104
hash_str = hasher .hexdigest ()
94
105
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 )
95
106
if socket_id :
96
- ret += "&socket_id=" + unicode (socket_id )
107
+ ret += "&socket_id=" + text_type (socket_id )
97
108
return ret
98
109
99
110
def send_request (self , signed_path , data_string , timeout = socket ._GLOBAL_DEFAULT_TIMEOUT ):
100
- http = httplib .HTTPConnection (self .pusher .host , self .pusher .port , timeout = timeout )
101
- http .request ('POST' , signed_path , data_string , {'Content-Type' : 'application/json' })
102
- resp = http .getresponse ()
111
+ client = httplib .HTTPConnection (self .pusher .host , self .pusher .port , timeout = timeout )
112
+ client .request ('POST' , signed_path , data_string , {'Content-Type' : 'application/json' })
113
+ resp = client .getresponse ()
103
114
return resp .status , resp .read ()
104
115
105
116
def authenticate (self , socket_id , custom_data = None ):
0 commit comments