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

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

问题描述

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

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

例如:

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

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

推荐答案

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

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);

在 Python 中:

and in Python:

import sys

who = sys.argv[1]

print "Hello %s" % who

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

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

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

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.

Python 脚本现在就所有意图和目的而言都是一个 Web 服务.您还可以在同一个 Web 服务接口下部署多个不同的脚本,并根据请求中发送的虚假 URI 来选择哪个脚本进行响应.

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.

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

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.

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

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.

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

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天全站免登陆