如何为网页制作 Python 解释器

How to make a Python interpreter for a webpage

可能已经有这样的问题,但其中 none 回答了我的问题。我有一个脚本,它在目录中加载一个 Python 脚本,然后显示带有 PHP:

的输出
<?php

$param1 = "first";
$param2 = "second";
$param3 = "third";

$command = "scripts/sg.py";
$command .= " $param1 $param2 $param3 2>&1";

header('Content-Type: text/html; charset=utf-8');
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"        />';
echo "<style type='text/css'>

</style>";

$pid = popen( $command,"r");

echo "<body><pre>";
while( !feof( $pid ) )
{
echo fread($pid, 256);
flush();
ob_flush();
echo "<script>window.scrollTo(0,99999);</script>";
usleep(100000);
}
pclose($pid);

echo "</pre><script>window.scrollTo(0,99999);</script>";
echo "<br /><br />Script finalizado<br /><br />";
?>

Python 代码应该 运行,位于目录中:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Script Python Example
import time
import sys

print "Initializing Python Script"

secret = 1337
guess = 0
count = 0
#Help the user give number range.
print 'Guess s number it`s between 0 <-> 2000 '
while guess != secret:
guess = input("Guess: ")
if guess < secret:
    print "to small"
elif guess > secret:
    print "to big"        
count += 1
print 'You guessed the number in %s try' % count

Python 确实有效!然而,Python 的输入似乎不起作用,它们会产生 EOF 错误(文件结束错误)。

任何人都可以帮助我并建议一种创建 Python 解释器的方法,该解释器 运行 是在目录中找到的 Python 文件。就像 skuplt.org,但不是 运行 为客户端用户编写代码,而是 运行 是位于目录中的 Python 文件,如上所述。

popen 打开一个单向管道;您可以读取或写入,但不能同时读取。

您想改用 proc_open - 请参阅 http://php.net/manual/en/function.proc-open.php