在 PHP 中整齐地显示命令提示符的输出

Neatly displaying an output from the command prompt in PHP

我希望在命令提示符中有 运行 一些命令后,整齐地显示 python 文件的输出。

目前,我的 PHP 代码的一部分如下所示:

$output2 = shell_exec('python amassprocess.py 2>&1' . $asnum);
echo $output2

它基本上将用户输入发送到 python 文件 amassprocess.py 并且 运行 将用户输入发送到文件。

amassprocess.py 文件当前如下所示:

import os, sys

#asnum = sys.argv[1]
asnum = "46489"

print("Here is a list of top level seed domain related to the AS Number provided:")
topdomain = os.system('cmd /c "amass.exe intel -asn {}"'.format(asnum))
for i in topdomain:
    print(i)
    print("<br>")

PS: 忽略2个asnum,其中一个用于测试

运行 宁 python 脚本的结果输出是这样的:

Here is a list of top level seed domain related to the AS Number provided:
ttvnw.net
justin.tv
twitch.tv
socialcam.com
Traceback (most recent call last):
  File "z:/xampp/htdocs/majorproject/amassprocess.py", line 8, in <module>
    for i in topdomain:
TypeError: 'int' object is not iterable

但是,我希望 运行 python 脚本的输出如下:

Here is a list of top level seed domain related to the AS Number provided:
ttvnw.net
<br>
justin.tv
<br>
twitch.tv
<br>
socialcam.com

这样当它显示在PHP时,它看起来像这样:

Here is a list of top level seed domain related to the AS Number provided:
ttvnw.net
justin.tv
twitch.tv
socialcam.com

目前在PHP中,结果全部显示在一行中,如下所示:

ttvnw.net justin.tv twitch.tv twitchcon.com socialcam.com Traceback (most recent call last): File "amassprocess.py", line 7, in for i in topdomain: TypeError: 'int' object is not iterable

如果有任何帮助或想法,我将不胜感激。谢谢。

您可以在 python 代码中执行以下操作。

  1. 定义一个变量来保存
    就像 x="<br>"
  2. 然后用于输出 print(i+x) 这将连接您的字符串。

使用您的代码,您可以将换行符替换为 <br />:

$output2 = shell_exec('python amassprocess.py 2>&1' . $asnum);
echo nl2br($output2);

或者你可以获得一组行并加入 <br />::

exec('python amassprocess.py 2>&1' . $asnum, $output2);
echo implode('<br />', $output2);

或者,使用您的代码,您可以在 <pre><code> 标签中显示,浏览器将使用换行符、制表符等呈现 pre-formatted:

$output2 = shell_exec('python amassprocess.py 2>&1' . $asnum);
echo "<pre>$output2</pre>";