From: Ben Darnell Date: Tue, 19 Mar 2013 00:52:23 +0000 (-0400) Subject: Fix draft76 websocket protocol; fix websocket demo for python 3. X-Git-Tag: v3.0.0~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8ddc760f18e490c81d64e6f71cabcf7bb758f071;p=thirdparty%2Ftornado.git Fix draft76 websocket protocol; fix websocket demo for python 3. --- diff --git a/demos/websocket/chatdemo.py b/demos/websocket/chatdemo.py index a62313936..92a0e1452 100755 --- a/demos/websocket/chatdemo.py +++ b/demos/websocket/chatdemo.py @@ -88,7 +88,8 @@ class ChatSocketHandler(tornado.websocket.WebSocketHandler): "id": str(uuid.uuid4()), "body": parsed["body"], } - chat["html"] = self.render_string("message.html", message=chat) + chat["html"] = tornado.escape.to_basestring( + self.render_string("message.html", message=chat)) ChatSocketHandler.update_cache(chat) ChatSocketHandler.send_updates(chat) diff --git a/tornado/websocket.py b/tornado/websocket.py index e87e7c9bf..aac477371 100644 --- a/tornado/websocket.py +++ b/tornado/websocket.py @@ -38,7 +38,7 @@ from tornado.ioloop import IOLoop from tornado.log import gen_log, app_log from tornado.netutil import Resolver from tornado import simple_httpclient -from tornado.util import bytes_type +from tornado.util import bytes_type, unicode_type try: xrange # py2 @@ -392,8 +392,9 @@ class WebSocketProtocol76(WebSocketProtocol): """Processes the key headers and calculates their key value. Raises ValueError when feed invalid key.""" + # pyflakes complains about variable reuse if both of these lines use 'c' number = int(''.join(c for c in key if c.isdigit())) - spaces = len(c for c in key if c.isspace()) + spaces = len([c2 for c2 in key if c2.isspace()]) try: key_number = number // spaces except (ValueError, ZeroDivisionError): @@ -438,7 +439,7 @@ class WebSocketProtocol76(WebSocketProtocol): if binary: raise ValueError( "Binary messages not supported by this version of websockets") - if isinstance(message, unicode): + if isinstance(message, unicode_type): message = message.encode("utf-8") assert isinstance(message, bytes_type) self.stream.write(b"\x00" + message + b"\xff")