セキュリティ的には微妙だけど、ひとつコマンドを打つためだけにSSHするのが面倒なので、よくサーバに内緒で仕込んでる。サンプルはlsだが、makeだって動いちゃう。
途中で\x0a
が出現すると<BR>
に置き換えて出力を見やすくしてるのがポイント。
#!/usr/bin/env python
import sys
from subprocess import Popen, PIPE
print "Content-Type: text/html\n\n"
p = Popen(["ls","-al"], stdout=PIPE)
while 1:
c = p.stdout.read(1)
if not c:
break
if c == bytes("\x0a"):
print "<br>"
sys.stdout.write(c)