开发者

How to avoid PHP exec() by using WSGI to execute a Python script?

Right now I am launching a Python script from PHP using exec() as I have to pass some dynamic variables from the website/MySQL to the command line. However, I would like to improve both the speed and the security of this operation so I thought of using WSGI. I was thinking that it might be possible to embed the Py开发者_JS百科thon script in a WSGI file, which would use the variables passed directly (or indirectly) from PHP...but how could I do this? I have never used WSGI so I don't even know if this is a stupid question or not :)


First off, can you do it all in either Python or PHP? Using multiple languages is normally seen as an anti-pattern. That's not to say it's always a bad idea, but you should be questioning why first, and only then if you're convinced it's a good idea move forward.

As far as the specifics of what you want to do, there are a few options:

  1. If using Apache, you can use the virtual() function to issue a new request through Apache.

    This is assuming that you are using mod_php and mod_wsgi. Basically, you'd build up a query string for the variables you want to pass, and call it like:

    virtual('path/to/python.py?'.$query);
    
  2. Write your own PHP WSGI gateway.

    Bascially, code the WSGI spec into a library, and call it explicitly from within PHP.

  3. Stick to calling it from exec() (which will likely be the best option for now).

The thing that you have to consider is that there's overhead inherent to cross-including between languages. You'll never get over that, it's just the nature of the beast. So instead, I'd suggest keeping the entire request inside of one environment, and dealing with it in the entirety in that environment...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜