开发者

PHP and Python interfacing

开发者 https://www.devze.com 2023-01-27 16:09 出处:网络
I have a Python application (command line tool running on a machine M1) that has input I1 and output O2. Also, I have a PHP application (website running on a machine M2) that feeds the Python applicat

I have a Python application (command line tool running on a machine M1) that has input I1 and output O2. Also, I have a PHP application (website running on a machine M2) that feeds the Python application with input I1 and expects to read the output O1. My question is what's the best approach to solve this problem? (the environment is GNU/Linux)

I was thinking at a solut开发者_Python百科ion with ssh; the PHP script executes a command via ssh: "ssh M2:./my_script.py arguments output_file" and transfers the output file "scp M2:output_file ." from M2 to M1. But, I don't think this solution is elegant. I was thinking of web services (the Python application should expose a web service), but I'm not sure what's the complexity of this solution or if it works.

Thanks,


Like others above, I'd do it by implementing a web service rather than fiddling with SSH. You're right that the SSH solution is inelegant.

The obvious answer for what to build your Python web application on top of: Django. If you want to use something lighter, look at a microframework like Flask.

For data interchange, JSON is your best bet -- supported by both Python and PHP now.

  • http://docs.python.org/library/json.html
  • http://php.net/manual/en/book.json.php


I think you should implement a web service . I don't know how to do it with python but i suppose it would be fairly easy.


Setup a small server on M1 using twisted (or something similar), that runs the command when hit with an xml request. Then return the ouput to the php side. So the php just sends a request with input1 to M1, and M1 returns a response with O1 (or O2? Your abbreviation are confusing :P). It's very KISS, and should scale fairly well. I have no idea how the php side would work, but twisted is extreamly well documented. Have a look at this link for some idea on how to set up a simple twisted server.


If O1 = O2 you could let them communicate with a binary protocol over a file-socket. Like Hessian or Thrift. Given that you run your apps on a *nix server it should be very fast.

0

精彩评论

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