PHP运行python文件, How to call Python file from within PHP?

<?php
    $command = escapeshellcmd('/usr/custom/test.py');
    $output = shell_exec($command);
    echo $output;
?>

 

PHP:escapeshellcmd()的用法_命令行函数

escapeshellcmd — shell 元字符转义

 

说明

string escapeshellcmd ( string $command )

escapeshellcmd() 对字符串中可能会欺骗 shell 命令执行任意命令的字符进行转义。 此函数保证用户输入的数据在传送到 exec() 或 system() 函数,或者 执行操作符 之前进行转义。

反斜线()会在以下字符之前插入: #&;`|*?~<>^()[]{}$, x0A 和 xFF。 ‘ 和 ” 仅在不配对儿的时候被转义。 在 Windows 平台上,所有这些字符以及 % 都会被空格代替。(译注:实际测试发现在 Windows 平台是前缀 ^ 来转义的。)

参数

command

要转义的命令。

返回值

转义后的字符串。

范例

Example #1 escapeshellcmd() example

<?php
// 我们故意允许任意数量的参数
$command = './configure '.$_POST['configure_options'];

$escaped_command = escapeshellcmd($command);
 
system($escaped_command);
?>
<?php

$url = 'https://v.qq.com/x/page/m0030tbxi5q.html';
$command = escapeshellcmd('python /var/www/html/x/videos/tentcent/Tencent-Video-Download-Parser/tencent-parse.py '.$url);
$output = shell_exec($command);
echo $output;

 

Warning

escapeshellcmd() 应被用在完整的命令字符串上。 即使如此,攻击者还是可以传入任意数量的参数。 请使用 escapeshellarg() 函数 对单个参数进行转义。

 

 

 

本文:PHP运行python文件, How to call Python file from within PHP?

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.