websocket_messaging in Python 3 #1696, thanks hirolau
This commit is contained in:
1 parent
dc29f33365
commit
a29947f298
1 file changed
+3
-25
@@ -94,32 +94,10 @@ import optparse
|
|||||||
import time
|
import time
|
||||||
import sys
|
import sys
|
||||||
import gluon.utils
|
import gluon.utils
|
||||||
|
from gluon._compat import to_native, to_bytes, urlencode, urlopen
|
||||||
if (sys.version_info[0] == 2):
|
|
||||||
from urllib import urlencode, urlopen
|
|
||||||
def to_bytes(obj, charset='utf-8', errors='strict'):
|
|
||||||
if obj is None:
|
|
||||||
return None
|
|
||||||
if isinstance(obj, (bytes, bytearray, buffer)):
|
|
||||||
return bytes(obj)
|
|
||||||
if isinstance(obj, unicode):
|
|
||||||
return obj.encode(charset, errors)
|
|
||||||
raise TypeError('Expected bytes')
|
|
||||||
else:
|
|
||||||
from urllib.request import urlopen
|
|
||||||
from urllib.parse import urlencode
|
|
||||||
def to_bytes(obj, charset='utf-8', errors='strict'):
|
|
||||||
if obj is None:
|
|
||||||
return None
|
|
||||||
if isinstance(obj, (bytes, bytearray, memoryview)):
|
|
||||||
return bytes(obj)
|
|
||||||
if isinstance(obj, str):
|
|
||||||
return obj.encode(charset, errors)
|
|
||||||
raise TypeError('Expected bytes')
|
|
||||||
|
|
||||||
listeners, names, tokens = {}, {}, {}
|
listeners, names, tokens = {}, {}, {}
|
||||||
|
|
||||||
|
|
||||||
def websocket_send(url, message, hmac_key=None, group='default'):
|
def websocket_send(url, message, hmac_key=None, group='default'):
|
||||||
sig = hmac_key and hmac.new(to_bytes(hmac_key), to_bytes(message)).hexdigest() or ''
|
sig = hmac_key and hmac.new(to_bytes(hmac_key), to_bytes(message)).hexdigest() or ''
|
||||||
params = urlencode(
|
params = urlencode(
|
||||||
@@ -138,8 +116,8 @@ class PostHandler(tornado.web.RequestHandler):
|
|||||||
if hmac_key and not 'signature' in self.request.arguments:
|
if hmac_key and not 'signature' in self.request.arguments:
|
||||||
self.send_error(401)
|
self.send_error(401)
|
||||||
if 'message' in self.request.arguments:
|
if 'message' in self.request.arguments:
|
||||||
message = self.request.arguments['message'][0]
|
message = self.request.arguments['message'][0].decode(encoding='UTF-8')
|
||||||
group = self.request.arguments.get('group', ['default'])[0]
|
group = self.request.arguments.get('group', ['default'])[0].decode(encoding='UTF-8')
|
||||||
print('%s:MESSAGE to %s:%s' % (time.time(), group, message))
|
print('%s:MESSAGE to %s:%s' % (time.time(), group, message))
|
||||||
if hmac_key:
|
if hmac_key:
|
||||||
signature = self.request.arguments['signature'][0]
|
signature = self.request.arguments['signature'][0]
|
||||||
|
|||||||
Reference in new issue
Block a user