>>> import json
>>> weird_json = '{"x": 1, "x": 2, "x": 3}'
>>> x = json.loads(weird_json)
>>> x
{u'x': 3}
>>> y = json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
>>> y
[u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
You can take the HTML data, and convert it into a dictionary, enabling you to do:
print x['x']
This is the starting point, create a socket in Python which listens to a port.
Then have it recieve data.
In Javascript, open a socket which can connect to a port (the one Python listens to).
Use, say: http://socket.io/
This is a pure socket-to-socket related issue?
A working relationship between Python and Javascript (on port 80):
from socket import *
import json
s = socket()
s.bind(('', 80))
s.listen(4)
ns, na = s.accept()
while 1:
try:
data = ns.recv(8192)
except:
ns.close()
s.close()
break
data = json.loads(data)
print data
There you got a socket listening to 80, connect to that and send whatever you want.
function callPython()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","Form-data",true);
xmlhttp.send();
}
For instance, where you can send the form data as a string, replacing "Form-data" and the response from Python can be put into "myDiv" :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…