2012-03-09 17 views
0

jsonからajaxにdomから送られたWebページから結果を取り出し、このデータをPythonスクリプトに送信して実行し、新しい結果をjsonとして返します。私はギアマンを実行しているPHPスクリプトは良い賭けになるだろうと言われたが、私はまだそれがどのように動作するのか分からない。Webサーバー経由でpythonスクリプトを実行し、結果をjavascriptに戻すにはどうすればよいですか?

+0

あなたは、システムのスクリプトやプログラムを呼び出すためにPHPを使用することができ、そしてそれは* *これらのスクリプトからそれに戻ってecho'd何かをキャプチャする必要があります。 '$ return = system(/path/to/script.py);'スクリプトがあなたのウェブサーバによって実行可能である限り。 –

+0

python cgiを作成し、post/getパラメータを読んでください。 – epascarello

+3

なぜPHPスクリプトを実行するのが良いアイデアのように見えますか?サーバー上でpythonスクリプトを実行するだけです。 – geoffspear

答えて

0

CGIディレクトリにPythonスクリプトを置き、スクリプト内のcgijsonモジュールを使用して、post/get paramsからAJAXを読み込みます。もちろんで、Pythonスクリプトを実行するためにPHPからシステムコールを行いますが、私はあなたがそうするべき理由を考えることはできません。

1

私の例はtwistedjqueryです。

#!/usr/local/bin/python 
import json 
import time 

from twisted.internet import reactor 
from twisted.web.server import Site 
from twisted.web.resource import Resource 


class DataPage(Resource): 
     isLeaf = True 

     def render_GET(self, request): 

       htmlFile = open("template.html") 
       html = open("template.html").read() 
       htmlFile.close() 

       return html 

     def render_POST(self, request): 
       print request.args 
       data = request.args['data'][0] 
       print data 
       return json.dumps(data[::-1]) 

resource = DataPage() 
factory = Site(resource) 
reactor.listenTCP(38123, factory) 
reactor.run() 

とhtml

<html> 
<head> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     function flipData(){ 

      $.post("http://localhost:38123/", { data: "makeitbackwards" }, 
      function(data){ 
        alert(data); 
      }, "json"); 
     } 
    </script> 
</head> 
<body> 
<a href="javascript:void(0)" onclick="flipData()">Get Time</a> 
</body> 
</html> 
関連する問題