fix serializers, websocket_messaging

This commit is contained in:
ilvalle committed 2016-06-13 20:20:49 +02:00
1 parent ab2cdd595b
commit a27f6f88ef
5 files changed
+35 -15

No files matched your search

+27 -7
View File
@@ -84,7 +84,6 @@ Tornado code inspired by http://thomas.pelletier.im/2010/08/websocket-tornado-re
"""
from __future__ import print_function
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
@@ -92,17 +91,38 @@ import tornado.web
import hmac
import sys
import optparse
import urllib
import time
import sys
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 = {}, {}, {}
def websocket_send(url, message, hmac_key=None, group='default'):
sig = hmac_key and hmac.new(hmac_key, message).hexdigest() or ''
params = urllib.urlencode(
sig = hmac_key and hmac.new(to_bytes(hmac_key), to_bytes(message)).hexdigest() or ''
params = urlencode(
{'message': message, 'signature': sig, 'group': group})
f = urllib.urlopen(url, params)
f = urlopen(url, to_bytes(params))
data = f.read()
f.close()
return data
@@ -121,7 +141,7 @@ class PostHandler(tornado.web.RequestHandler):
print('%s:MESSAGE to %s:%s' % (time.time(), group, message))
if hmac_key:
signature = self.request.arguments['signature'][0]
if not hmac.new(hmac_key, message).hexdigest() == signature:
if not to_bytes(hmac.new(to_bytes(hmac_key), to_bytes(message)).hexdigest()) == signature:
self.send_error(401)
for client in listeners.get(group, []):
client.write_message(message)
@@ -140,7 +160,7 @@ class TokenHandler(tornado.web.RequestHandler):
message = self.request.arguments['message'][0]
if hmac_key:
signature = self.request.arguments['signature'][0]
if not hmac.new(hmac_key, message).hexdigest() == signature:
if not to_bytes(hmac.new(to_bytes(hmac_key), to_bytes(message)).hexdigest()) == signature:
self.send_error(401)
tokens[message] = None