Python:关闭Web响应

时间:2014-06-11 06:06:54

标签: python apache2

通常,在apache cgi-bin文件中,您以这种方式响应数据:

print "Content-Type:text/html"
print 
print "Hello world"

一旦脚本完成,浏览器就会显示结果。但是如何关闭响应(告诉客户端没有其他任何内容并关闭,就像脚本结束一样)所以Web浏览器显示内容并且python可以继续做其他事情?

print "Content-Type:text/html"
print 
print urllib.urlopen("staticpage.html").read()
close() ?
do_a_backend_function # browser is not waiting and has showed the static page to the user

我认为在第一种情况下,apache正在处理这个问题,但在第二种情况下,我想强迫它。感谢

按照@furas指示,我猜我可以做:

print "Content-Type:text/html"
print 
print urllib.urlopen("staticpage.html").read()
import sys
sys.stdout.close()
do_a_backend_function() # browser is not waiting and has showed the static page to the user

而不是使用多线程,这是很难开发的。但我还没试过,希望有人这样做。

1 个答案:

答案 0 :(得分:0)

print将文字发送至sys.stdout,以便您可以尝试刷新并关闭sys.stdout

但我不知道接下来会发生什么。也许Apache会因为sys.stdout关闭而终止您的流程。