【问题标题】:print output to webpage as soon as the print statement is executed in Python CGI script在 Python CGI 脚本中执行打印语句后立即将输出打印到网页
【发布时间】:2017-04-14 00:46:49
【问题描述】:

我想在执行打印语句后立即在网页上打印输出。这是我的代码:

#!/usr/bin/python2.7
import sys, time    
sys.stdout.write('Content-Type: text/html\n')
sys.stdout.write('\n\n')
print "<html><body>"
print "hi"
sys.stdout.flush()
for i in range(10):
    print '<div>%i</div>'%i
    sys.stdout.flush()
    time.sleep(1)
print "</body></html>"

但我只有在执行完整脚本后才能立即看到所有输出。我错过了什么吗?

【问题讨论】:

  • 请参阅stackoverflow.com/questions/1645308/… 了解更多信息。 (它使用 Perl,但基本原则仍然适用。)
  • @John Gordon,这很有帮助。
  • 我仍然无法解决这个问题, sys.stdout.flush 在我的情况下似乎不起作用,如果有人可以提出任何其他解决方案,那就太好了?
  • 我认为问题在于您的浏览器会等到它得到所有输出后才显示任何内容。
  • 我尝试了所有 3 个,firefox,chrome 和 IE,但在它们中没有一个,它工作。不知道有什么解决办法。

标签: python python-2.7 python-3.x cgi flush


【解决方案1】:

您可能需要禁用需要在将整个内容发送到浏览器之前缓冲整个内容的 apache 压缩。

尝试将其添加到/etc/apache2 配置文件中:

<Directory /var/www/your_web_site_here>
    SetEnv no-gzip 1
</Directory>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    相关资源
    最近更新 更多