这里有一个小脚本proc_open
如上所述,要向python脚本发送一个简单的文本变量,请执行以下操作:
add_shortcode( \'execute_python\', \'execute_python_with_argv\' );
function execute_python_with_argv( $attributes ){
$description = array (
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w") // stderr
);
$application_system = "python ";
$application_path .= plugin_dir_path( __FILE__ );
$application_name .= "hello.py";
$separator = " ";
$application = $application_system.$application_path.$application_name.$separator;
$argv1 = \'"output to receive back from python script"\';
$pipes = array();
$proc = proc_open ( $application.$argv1 , $description , $pipes );
//echo proc_get_status($proc)[\'pid\'];
if (is_resource ( $proc ))
{
echo "Stdout : " . stream_get_contents ( $pipes [1] ); //Reading stdout buffer
fclose ( $pipes [1] ); //Closing stdout buffer
fclose ( $pipes [2] ); //Closing stderr buffer
$return_value = proc_close($proc);
echo "<br/>command returned: $return_value<br/>";
}
$application_test = glitch_player_DIR.$application_name;
echo "<br/>Is ".$application_test." executable? ".is_executable($application_test)." ";
echo "readable? ".is_readable($application_test)." ";
echo "writable? ".is_writable($application_test)." ";
} //EOF main/shortcode function
在底部添加了一些测试,以查看python文件是否
rwx
. 我认为发送
argv
将使用fwrite,但它对我来说不起作用
this tutorial.
这是我使用的python脚本。正如上面的评论所指出的,类似于#!/usr/bin/env python
可能需要,具体取决于服务器。
#!/usr/bin/env python
from sys import argv
script, what_he_said = argv
print "This is what you submitted: %s \\n \\n Isn\'t that amazing, man? " % what_he_said