[英]Running python CGI script on browser generating script code not expected output
[英]Python cgi script displays output in the browser only after its completion
我注意到python cgi腳本僅在完成執行后才顯示輸出。有沒有辦法,我可以使該腳本繼續執行並連續打印結果。 我發現,如果我在Apache中這樣運行腳本“ abc.py”,則會連續顯示輸出(腳本繼續執行),但是從另一個腳本調用腳本時不起作用。
下面是代碼:-(abc.py)
import sys, time
#sys.stdout.write('Content-Type: text/html;charset=utf-8\r\n\r\n')
#print '<html><body>'
for i in range(10):
print '<div>%i</div>'%i
sys.stdout.flush()
time.sleep(1)
我想從另一個CGI腳本運行此腳本。 現在,當我調用此腳本(在Apache中使用以下腳本)時,僅在完成后才輸出輸出。 有沒有辦法解決它。
print 'Content-Type: text/html;charset=utf-8\r\n\r\n'
print '<html><body>'
PIPE = subprocess.PIPE
pd = subprocess.Popen(['abc.py'],
stdout=PIPE, stderr=PIPE)
stdou, stder = pd.communicate()
sys.stdout.write(stdou)
sys.stdout.flush()
工作代碼
abc.py
#!/grid/common/bin/python
import sys, time
for i in range(10):
print str(i)
sys.stdout.flush()
time.sleep(1)
主腳本
#!/grid/common/bin/python
import sys, time
import subprocess
import cgi, cgitb
cgitb.enable()
print 'Content-Type: text/html;charset=utf-8\r\n\r\n'
print '<html><body>'
PIPE = subprocess.PIPE
pd = subprocess.Popen(['abc.py'],
stdout=PIPE, stderr=PIPE)
while True:
output = pd.stdout.read(1)
if output == '' and pd.poll() != None:
break
if output != '':
sys.stdout.write(output)
sys.stdout.flush()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.