代码内容:
from subprocess import PIPE,Popen,STDOUT @celery_app.task def call_shell(ipList,add_ipList,shell_log,shell_dir,shell_file): os.popen(r"echo %s >> %s" % (ipList, add_ipList)) # output = os.popen("cd %s;/bin/sh %s" % (shell_dir, shell_file)) p = Popen("cd %s;/bin/sh %s" % (shell_dir, shell_file),stdout=PIPE,stderr=STDOUT,shell=True) for line in iter(p.stdout.readline,""): shell_log.write(line) shell_log.flush() shell_log.close() os.popen(r"/bin/sed -i '$d' %s" % add_ipList) 在这里我用了 subprocess.Popen 去执行一个 shell 脚本,然后 shell 执行的时候会有一大串内容输出,我直接用了p.stdout.readline读取它到一个 txt 文件中,这个文件我弄成了一个 url 去访问他: http://192.168.1.21/shell_log.txt
最后的问题在于,只有当 shell 内容全部读取后,我才能在页面刷新出内容的。
有没有什么办法, shell 输出一点,我就能在 http://192.168.1.21/shell_log.txt 访问到一点的。
就是有点类似需要实时的看到 shell 日志输出的。而不是当整个 shell 输出完, p.stdout.readline 全部读取完后才能在页面看到的。
