开发者

is json the answer to this: python program will talk and javascript will listen?

the same problem haunting me a month ago is still haunting me now. i know ive asked several questions regarding this on this site and i am truly sorry for that. your suggestions have all been excellent but the answer is still elusive. i now realize that this is a direct result of me not being able to phrase my question properly and for that i am sorry.

to give you guys a generalized view of things, here i go: the situation is like this, i have 2 server side scripts that i want to run.

  1. a python program/script that continuously spouts some numbers

  2. based on the output from that python script, a javascript script will perform some action on a webpage (e.g., change background color, display alert message, change some text)

ive studied the replies to my previous posts and have found that what i want to accomplish is more or less accomplished by json. it is my understanding that json transforms 'program-specific' variables into a format that is more 'standard or general or 开发者_运维问答global'. two different programs therefore now have the means to 'talk' with each other because they are now speaking the same 'language'.

the problem is then this, how do i actually facilitate their communication? what is the 'cellphone' between these server side scripts? do they even need one?

thank you!


If I understand what you're asking, the "cellphone" is TCP/IP. The javascript is not server-side; it runs on the client side, and alters what the client's browser displays based on json data that it downloads from the server -- data that in this case is generated by Python.

This question provides a relevant example, though it's a bit technical: JSON datetime between Python and JavaScript

Here's a very basic tutorial that explains how to create a dynamic webpage using python and javascript. It doesn't appear to use json, but it should familiarize you with the fundamentals. Once you understand what's there, using json to transport more complicated data should be fairly straightforward.

http://kooneiform.wordpress.com/2010/02/28/python-and-ajax-for-beginners-with-webpy-and-jquery/


I assume you mean: Python is on the web server, and Javascript is running in the client's web browser.

Because browsers are all different (IE6 is terrible, Chrome is great), there are a huge number of ways people found to "hack" this "cellphone" into place. These techniques are called AJAX and COMET techniques. There is no one "cellphone", but a whole bunch of them! Hopefully, you can find a library to select the right technique for the browser, and you just have to worry about the messages.

Comet is harder to do, but lets the server "push" messages to the client.

Ajax can be easier - you just periodically "pull" messages from the server.

Start with Ajax, then look at comet if you really need it. Just start by have the client (javascript) make a "GET" request, to see if the number has changed.


I don't know Javascript or json, but... if you've ever seen an Unix-like operating system, you know about pipes. Like program1 | program2 | program3 ... Why don't you just connect Python and Javascript programs with pipes? The first one writes to stdout, and the next one reads from stdin.


This probably isn't the answer that you are looking for, and without links to your previous posts, I don't have much to go on, but nonetheless...

javascript is client side. I can interpret your question 2 different ways...

  1. Your python script is running on your computer, and you want a script to actually alter your current browser window.

    Not too sure, but writing a browser plugin may be the answer here.

  2. Your python script is running on the server, and as a result of the script running, you want the display of your site to be changed for viewing persons.

    In this case, you will could use ajax polling (or similar) on your site. Have your site be polling the server with ajax, call a server method that checks the output of the script (maybe written to a file?), and see if it has changed.


When 2 process need to communicate, they need to decide of a common/shared way to express things and a protocol to exchange those things.

In your case, since one of the processes is a browser, the protocol of choice is http. So the browser needs to do an http request or regular http request to your python process.

This python process Will need in Some way or another to be exposed via http.

There are several ways to build a web server in python. You should read this article : http://fragments.turtlemeat.com/pythonwebserver.php as a jumpstart.

Once you have this, your browser Will be able to issue HTTP GET requests to your server and your server can reply with a string.

This string can be whatever you like. Nevertheless if your answer contains structured data it can be a good start to use the XML notation or the json notation.

Json (stands for Javascript object notation) is very easy to use in javascript and this is why many people advised you to choose this notation.

I hope this will help you

Jérome wagner

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜