0

I'm using python socket to communicate with JavaScript clients.

http://docs.python.org/2/library/socket.html

And following the part of JavaScript code,

ws = new WebSocket("ws://localhost:7777/");
ws.onopen = function(){
  alert("websocket opened.");
}
ws.onclose = function(){
  alert("websocket closed.");
}
ws.onmessage = function(e) {
  alert(e.data);
}

I need to pass a json or a string from the python socket to the JavaScript. Still cant find a way to pass a json. Not any encoding mechanism support for that.

3
  • You can try Juggernaut: flask.pocoo.org/snippets/80 Commented Dec 7, 2012 at 12:07
  • @Blender - I would steer clear of Juggernaut since it has been deprecated by the person that wrote it Commented Dec 7, 2012 at 12:14
  • @D.Shawley How I know at this moment socketio lib is a standart cause it's use whole available transports and choice best one available Commented Dec 7, 2012 at 12:19

3 Answers 3

2

Since you are using WebSockets on the client side, your Python code needs to use the Web Socket protocol to send a message to the client. Take a look at the answers to javascript - WebSocket Server in Python for a number of good alternatives.

Short Answer: you can't use socket and send messages to a client across a Web Socket without implementing the Web Socket protocol.

Sign up to request clarification or add additional context in comments.

Comments

1

In python you can use the JSON module. And in JavaScript you can use jQuery.parseJSON

Example from the python doc:

>>> import json
>>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
'["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> print json.dumps("\"foo\bar")
"\"foo\bar"
>>> print json.dumps(u'\u1234')
"\u1234"
>>> print json.dumps('\\')
"\\"
>>> print json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)
{"a": 0, "b": 0, "c": 0}

Example from jQuery doc:

var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );

3 Comments

The serialization above isn't to blame for that I think
@ChamithMalinda Lol, this not problem python or js this is just you MUST properly implement websocket protocol work on you server side.
The problem is not the JSON, the problem is sending the string to the client. You need something to implement Web Socket in there.
0

Hmm, as a fact you can't send python's object to your client's js. But you can send valid json in the string and parse it on client by jQuery.parseJSON for example.

2 Comments

I can't send any thing either a string from the python socket.
@ChamithMalinda I answer to you above what socket it's low level lib for work with tcp/ip protocol it's just can open socket or listen it and send various messages to another. BUT if you need WEBSOCKETS you must implement it on your server side on top of the socket lib. OR you can use allready works frameworks like tornado or gevent wich allready support operations with websockets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.