apache Python未正确刷新

efzxgjgh  于 2022-12-27  发布在  Apache
关注(0)|答案(1)|浏览(123)

我尝试刷新内容,因为它是由Linux服务器中的Python脚本生成的。它没有按预期工作。我生成这个Test.py是为了进行概念验证:

#!/usr/bin/python3 -u
from cgitb import enable
from time import sleep
from os import environ
import datetime
import sys

environ["PYTHONUNBUFFERED"] = "true"

enable()
print("Content-type: text/html;charset=utf-8", flush=True)
print("", flush=True)
sys.stdout.flush()

print("<html><head>")
print("")
print("</head><body>")

Count=10
while (Count>0):
    print('Generated with print - at:'+str(datetime.datetime.now())+' and received at <script>document.write(Date());</script><br>', flush=True)
    sys.stdout.write('Generated with stdout - at:'+str(datetime.datetime.now())+' and received at <script>document.write(Date());</script><br>')
    sys.stdout.flush()
    Count=Count-1
    sleep(1)

print("</body></html>")

正如您在代码中所看到的,我尝试了所有的技术,以获得一个漂亮的同花顺,但我得到的是:

Generated with print - at:2022-12-25 12:19:52 and received at Sun Dec 25 2022 12:20:02
Generated with stdout - at:2022-12-25 12:19:52 and received at Sun Dec 25 2022 12:20:02
Generated with print - at:2022-12-25 12:19:53 and received at Sun Dec 25 2022 12:20:02
Generated with stdout - at:2022-12-25 12:19:53 and received at Sun Dec 25 2022 12:20:02
Generated with print - at:2022-12-25 12:19:54 and received at Sun Dec 25 2022 12:20:02
Generated with stdout - at:2022-12-25 12:19:54 and received at Sun Dec 25 2022 12:20:02
Generated with print - at:2022-12-25 12:19:55 and received at Sun Dec 25 2022 12:20:02
Generated with stdout - at:2022-12-25 12:19:55 and received at Sun Dec 25 2022 12:20:02
Generated with print - at:2022-12-25 12:19:56 and received at Sun Dec 25 2022 12:20:02
Generated with stdout - at:2022-12-25 12:19:56 and received at Sun Dec 25 2022 12:20:02
Generated with print - at:2022-12-25 12:19:57 and received at Sun Dec 25 2022 12:20:02
Generated with stdout - at:2022-12-25 12:19:57 and received at Sun Dec 25 2022 12:20:02
Generated with print - at:2022-12-25 12:19:58 and received at Sun Dec 25 2022 12:20:02
Generated with stdout - at:2022-12-25 12:19:58 and received at Sun Dec 25 2022 12:20:02
Generated with print - at:2022-12-25 12:19:59 and received at Sun Dec 25 2022 12:20:02
Generated with stdout - at:2022-12-25 12:19:59 and received at Sun Dec 25 2022 12:20:02
Generated with print - at:2022-12-25 12:20:00 and received at Sun Dec 25 2022 12:20:02
Generated with stdout - at:2022-12-25 12:20:00 and received at Sun Dec 25 2022 12:20:02
Generated with print - at:2022-12-25 12:20:01 and received at Sun Dec 25 2022 12:20:02
Generated with stdout - at:2022-12-25 12:20:01 and received at Sun Dec 25 2022 12:20:02

我真的不知道该怎么做,我试过在windows客户端上使用chrome,firefox和edge。

EDIT我按照AKX的要求添加了适当的html标签。

tjvv9vkg

tjvv9vkg1#

您的代码似乎工作正常:它每秒打印一次输出。所以问题是:你还想怎么样?
如果您希望Web浏览器显示脚本的进度,请阅读以下内容:Show progress of PYTHON CGI script

相关问题