5
我使用龍捲風作爲服務器。我希望它能接收二進制數據。服務器端很簡單:龍捲風支持二進制的websockets
import tornado.websocket
import tornado.httpserver
import tornado.ioloop
import tornado.web
class WebSocketServer(tornado.websocket.WebSocketHandler):
def open(self):
print 'OPEN'
def on_message(self, message):
print 'GOT MESSAGE: {}'.format(message)
def on_close(self):
print 'CLOSE'
app = tornado.web.Application([
(r'/', WebSocketServer)
])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(9500)
tornado.ioloop.IOLoop.instance().start()
該服務器僅用於可視化傳入數據,不是太特殊。服務器的工作原理是使用標準ascii查找,但當它獲取任何unicode(我對假二進制數據的測試)時會發生爆炸。我使用的網站http://www.websocket.org/echo.html並重定向發送到ws://172.0.0.1:9500/
這是我設置服務器的地方。然後,服務器促使我用很討厭的錯誤:
ERROR:tornado.application:Uncaught exception in/
Traceback (most recent call last):
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/tornado/websocket.py", line 303, in wrapper
return callback(*args, **kwargs)
File "test.py", line 11, in on_message
print 'GOT MESSAGE: {}'.format(message)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa1' in position 0: ordinal not in range(128)
的角色是¡
,顛倒!現在我知道龍捲風可以send binary,但顯然沒有收到?我可能會犯一些小錯誤,但它在哪裏?
什麼發生,如果您將print語句替換爲'print u'GOT MESSAGE:{}'。format(message)'? – falsetru
@falsetru修復它。嘆氣,我要關閉我的筆記本電腦,並反覆敲打我的頭靠在最近的牆上。感謝您指出了這一點。 – jakebird451