如何从 Apache 2 提供无缓冲的 CGI 内容?

发布于 2024-07-29 18:21:05 字数 806 浏览 2 评论 0原文

我希望能够允许用户在生成长时间运行的 GCI 脚本时查看其输出,而不是在脚本完成后查看。 然而,即使当我显式刷新 STDOUT 时,服务器似乎也会等待脚本完成,然后再将响应发送给客户端。 这是在运行 Apache 2.2.9 的 Linux 服务器上。

python CGI 示例:

#!/usr/bin/python

import time
import sys


print "Content-type: text/plain"
print
for i in range(1, 10):
        print i
        sys.stdout.flush()
        time.sleep(1)

print "Done."

perl 中的类似示例:

#!/usr/bin/perl

print "Content-type: text/plain\n\n";

for ($i = 1; $i <= 10 ; $i++) {
        print "$i\n";
        sleep(1);
}

print "Done.";

此链接表示从 Apache 1.3 开始 CGI 输出应该是无缓冲的(但这可能仅适用于 Apache 1.x): http://httpd.apache.org/docs/1.3/misc/FAQ-F.html#nph-scripts

有任何想法吗?

I would like to be able to allow a user to view the output of a long-running GCI script as it is generated rather than after the script is complete. However even when I explicitly flush STDOUT the server seems to wait for the script to complete before sending the response to the client. This is on a Linux server running Apache 2.2.9.

Example python CGI:

#!/usr/bin/python

import time
import sys


print "Content-type: text/plain"
print
for i in range(1, 10):
        print i
        sys.stdout.flush()
        time.sleep(1)

print "Done."

Similar example in perl:

#!/usr/bin/perl

print "Content-type: text/plain\n\n";

for ($i = 1; $i <= 10 ; $i++) {
        print "$i\n";
        sleep(1);
}

print "Done.";

This link says as of Apache 1.3 CGI output should be unbuffered (but this might apply only to Apache 1.x): http://httpd.apache.org/docs/1.3/misc/FAQ-F.html#nph-scripts

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人海汹涌 2024-08-05 18:21:05

Randal Schwartz 的文章 通过 CGI 观看长进程 解释了一种不同的方式(恕我直言,更好)观察长时间运行过程的方式。

Randal Schwartz's article Watching long processes through CGI explains a different (and IMHO, better) way of watching a long running process.

你的笑 2024-08-05 18:21:05

刷新 STDOUT 会有所帮助。 例如,以下 Perl 程序应该按预期工作:

#!/usr/bin/perl

use strict;
use warnings;

local $| = 1;

print "Content-type: text/plain\n\n";

for ( my $i = 1 ; $i <= 10 ; $i++ ) {
    print "$i\n";
    sleep(1);
}

print "Done.";

Flushing STDOUT can help. For example, the following Perl program should work as intended:

#!/usr/bin/perl

use strict;
use warnings;

local $| = 1;

print "Content-type: text/plain\n\n";

for ( my $i = 1 ; $i <= 10 ; $i++ ) {
    print "$i\n";
    sleep(1);
}

print "Done.";
青朷 2024-08-05 18:21:05

您必须将推送脚本放入包含特殊 .htaccess 的特殊目录中
在此环境规格下:

Options +ExecCGI
AddHandler cgi-script .cgi .sh .pl .py
SetEnvIfNoCase Content-Type \
"^multipart/form-data;" "MODSEC_NOPOSTBUFFERING=Do not buffer file uploads"
SetEnv no-gzip dont-vary

You must put your push script into a special directory wich contain a special .htaccess
with this environnment specs:

Options +ExecCGI
AddHandler cgi-script .cgi .sh .pl .py
SetEnvIfNoCase Content-Type \
"^multipart/form-data;" "MODSEC_NOPOSTBUFFERING=Do not buffer file uploads"
SetEnv no-gzip dont-vary
喜爱纠缠 2024-08-05 18:21:05

根据 CGI::Push

Apache Web 服务器版本 1.3b2
on 不需要服务器推送脚本
作为 NPH 脚本安装:-nph
do_push() 的参数可以设置为
false 值来禁用额外的
NPH 脚本所需的标头。

你只需要在 python 中找到 do_push 等效项即可。

编辑:查看CherryPy:流式传输响应正文

当您将配置条目“response.stream”设置为 True(并使用“yield”)时,CherryPy 会像这样管理 HTTP 服务器和代码之间的对话:

替代文字
(来源:cherrypy.org

According to CGI::Push,

Apache web server from version 1.3b2
on does not need server push scripts
installed as NPH scripts: the -nph
parameter to do_push() may be set to a
false value to disable the extra
headers needed by an NPH script.

You just have to find do_push equivalent in python.

Edit: Take a look at CherryPy: Streaming the response body.

When you set the config entry "response.stream" to True (and use "yield") CherryPy manages the conversation between the HTTP server and your code like this:

alt text
(source: cherrypy.org)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文