将值从 PHP 脚本传递到 Python 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4977125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing value from PHP script to Python script
提问by stackVidec
I looked at the other questions similar to this one, but can't figure this out still.
我查看了与此类似的其他问题,但仍然无法弄清楚。
I have a basic php file that does this:
我有一个基本的 php 文件可以执行此操作:
?php
$item='example';
$tmp = exec("python testscriptphp.py .$item");
echo $tmp;
?
While succesfully calls python that I have running on my webhostserver. Now in my python script i want something like this:
虽然成功调用了我在我的网络主机服务器上运行的 python。现在在我的 python 脚本中,我想要这样的东西:
item=$item
print item
Basically I'm asking how to pass variables from PHP to a python script and then back to php if necessary.
基本上我问的是如何将变量从 PHP 传递到 python 脚本,然后在必要时返回到 php。
Thanks!
谢谢!
回答by Uku Loskit
Although netcoder pretty much gave you the answer in his comment, here's an example:
尽管 netcoder 在他的评论中几乎给了你答案,但这里有一个例子:
Python->PHP
Python->PHP
example.py
例子.py
import os
os.system("/usr/bin/php example2.php whatastorymark")
example2.php
例子2.php
<?php
echo $argv[1];
?>
PHP->Python
PHP->Python
<?php
$item='example';
$tmp = exec("python testscriptphp.py .$item");
echo $tmp;
?>
testscriptphp.py
测试脚本php.py
import sys
print sys.argv[1]
Here's how PHP's command line argument passing works: http://php.net/manual/en/reserved.variables.argv.phpThe same for Python: http://docs.python.org/library/sys.html#sys.argv
以下是 PHP 命令行参数传递的工作原理:http: //php.net/manual/en/reserved.variables.argv.phpPython 相同:http: //docs.python.org/library/sys.html#sys .argv
回答by Mani Kandan
write a php file example index.php:
写一个php文件示例index.php:
<?PHP
$sym = $_POST['symbols'];
echo shell_exec("python test.py .$sym");
?>
$sym is a parameter we passining to test.py python file. then create a python example test.py:
$sym 是我们传递给 test.py python 文件的参数。然后创建一个python示例test.py:
import sys
print(sys.argv[1])
I hope it help you.
我希望它能帮助你。

