8
8

More than 5 years have passed since last update.

Webサーバ上でコマンドを実行して結果を表示する

Posted at

セキュリティ的には微妙だけど、ひとつコマンドを打つためだけに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)

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8