Lennart Kloppenburg Lennart Kloppenburg - 1 year ago 182
Python Question

Python Eventlet + Javascript Websocket returns Blob

I am experimenting with Python's Eventlet Websocket support, using this simple example :

import eventlet
from eventlet import wsgi
from eventlet import websocket
from eventlet.support import six

# demo app
import os
import json
import time
import random


@websocket.WebSocketWSGI
def handle(ws):
""" This is the websocket handler function. Note that we
can dispatch based on path in here, too."""
if ws.path == '/echo':
while True:
ws.send("hello")
#ws.send(json.dumps({"msg":"hello"}))
time.sleep(1)

elif ws.path == '/data':
for i in six.moves.range(10000):
ws.send("0 %s %s\n" % (i, random.random()))
eventlet.sleep(0.1)


def dispatch(environ, start_response):
""" This resolves to the web page or the websocket depending on
the path."""
if environ['PATH_INFO'] == '/data':
return handle(environ, start_response)
else:
start_response('200 OK', [('content-type', 'text/html')])
return [open(os.path.join(
os.path.dirname(__file__),
'websocket.html')).read()]

if __name__ == "__main__":
# run an example app from the command line
listener = eventlet.listen(('127.0.0.1', 7000))
print("\nVisit http://localhost:7000/ in your websocket-capable browser.\n")
wsgi.server(listener, dispatch)


I'm not going to include the entire websocket handler I have in the Javascript, just the
ws.onmessage
method:

ws.onmessage = function (evt)
{
console.log(evt.data)
var received_msg = evt.data;
#Do stuff, i.e var obj = JSON.parse(received_msg)
#callback(obj)
};


The
console.log(evt.data)
indicates a succesful connection with the websocket (and you can assume this is all fine). However, the logging shows
Blob {size: 31, type: ""}
as the content of
evt.data
. I assume this is some kind of response object which is interpreted as binary (file) data (though I might be entirely wrong), but I'm not sure what to do with this.

I see that Blob data is often the data type for file-like objects. I suppose I could approach it as such, but I really only want to send json data back and forth. I've tried dumping a dict as a JSON and sending that, but it did the same. Even a string is received in Blob format.

How do I use
eventlet
for json data transmission?

Answer Source

As of 2017-05 Eventlet websocket API does not support string websocket frames. Patches are welcome, it's easy.

Your options:

  • read Blob at javascript end FileReader.readAsText
  • use another websocket library (pure Python implementation will work fine with Eventlet patching)
  • add string frame support to Eventlet websocket library
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download