如何将变量从PHP传递给Python? [英] How can I pass variable from PHP to Python?

查看:76
本文介绍了如何将变量从PHP传递给Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将变量从.php脚本传递给Python,反之亦然吗?

例如:

//myPHPScript.php 
$hello = 'hello'; 

//myPythonScript.py
print(get the result from $hello variable found in myPHPScript.php) 

解决方案

取决于您如何调用Python脚本.例如,如果通过system()进行此操作,则可以将其放在参数中:

$hello = 'world';

$result = shell_exec('/path/to/python /path/to/your/script.py ' . $hello);

和在Python中:

import sys

who = sys.argv[1]

print "Hello %s" % who

现在PHP中的$result将包含"Hello world".

一种更有表现力的可能性(并非总是可能,但值得考虑)是一种"fastcgi方法";您的Python脚本实际上始终在运行,并且在端口8888上接受套接字连接(例如,使用HTTPlib).那时,您可以使用cURL从PHP连接到http://127.0.0.1:8888,并发送以JSON编码的结构化数据(因为Python具有JSON)解码器;我不太确定PHP的反序列化程序),并通过同一路径返回信息.

Python脚本现在已成为一种Web服务.您还可以在同一个Web服务界面下部署多个不同的脚本,然后根据请求中发送的伪URI选择要回答的脚本.

使用这种方法,您需要检查请求之间的状态是否正确隔离,即代表Peter请求数据处理不会导致返回属于Paul的数据;或正在处理的所有数据都不安全,即不需要安全性或身份验证.

此方法的另一个优点是缓存-python脚本在从PHP发出的请求之间保持活动状态,并且如果可行的话,无需重新计算任何内容即可返回已知问题的相同答案.有一些可以插入的Python缓存框架.

另一个优势是,您可以通过将Python服务部署到另一台计算机(不一定从更广泛的Internet上可以访问),甚至是几台不同的计算机上,轻松地扩展此方法.

Could I pass a variable from a .php script to Python and vice versa?.

For example:

//myPHPScript.php 
$hello = 'hello'; 

//myPythonScript.py
print(get the result from $hello variable found in myPHPScript.php) 

解决方案

Depends on how you invoke the Python script. If you do that via system() for example, you can put it in the arguments:

$hello = 'world';

$result = shell_exec('/path/to/python /path/to/your/script.py ' . $hello);

and in Python:

import sys

who = sys.argv[1]

print "Hello %s" % who

Now $result in PHP will contain "Hello world".

A more performant possibility, not always possible but worth considering, is a sort of "fastcgi approach"; your Python script is actually always running and accepts socket connections (e.g. using HTTPlib) on, say, port 8888. At that point you can connect from PHP using cURL to http://127.0.0.1:8888 and send structured data encoded in JSON (since Python has a JSON decoder; I'm not so sure about a PHP unserializer), and return information via the same path.

The Python script is now, to all intents and purposes, a web service. You can also deploy several different scripts under the same web service interface and choose which one will answer based on a fake URI sent in the request.

With this approach you need to check that the state between requests is properly isolated, i.e., requesting data processing on behalf of Peter won't result in data being returned that belong to Paul; or that all data being processed is insecure, that is, it requires no security or authentication.

One other advantage of this approach is caching - the python script stays alive between requests being made from PHP, and can return the same answer to a known question with no need of recalculating anything, if this is doable. There are caching frameworks for Python that are ready to plug in.

An additional advantage is that you can easily scale this approach by deploying the Python service on a different machine (not necessarily reachable from the wider Internet), or even several different machines.

这篇关于如何将变量从PHP传递给Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆