如何在服务器推送 Perl CGI 程序中发送多个图像?

发布于 2024-09-02 08:30:18 字数 739 浏览 4 评论 0原文

我是 Perl CGI 等方面的初学者。我正在用一段 Perl 代码尝试服务器推送概念。它应该每三秒向客户端发送一个 jpeg 图像。

不幸的是,似乎没有任何作用。有人可以帮助找出问题吗?

这是代码:

use strict;
# turn off io buffering
$|=1;
print "Content-type: multipart/x-mixed-replace;";
print "boundary=magicalboundarystring\n\n";
print "--magicalboundarystring\n";

#list the jpg images
my(@file_list) = glob "*.jpg";
my($file) = "";

foreach $file(@file_list ) 
{
     open FILE,">", $file or die "Cannot open file $file: $!";
     print "Content-type: image/jpeg\n\n";

    while ( <FILE> )
    { 
        print "$_";
    }

    close FILE;
     print "\n--magicalboundarystring\n";
     sleep 3;
    next;

}

编辑:添加关闭 i/o 缓冲,添加“use strict”和“@file_list”,“$file”设为本地

I am a beginner in Perl CGI etc. I was experimenting with server-push concept with a piece of Perl code. It is supposed to send a jpeg image to the client every three seconds.

Unfortunately nothing seems to work. Can somebody help identify the problem?

Here is the code:

use strict;
# turn off io buffering
$|=1;
print "Content-type: multipart/x-mixed-replace;";
print "boundary=magicalboundarystring\n\n";
print "--magicalboundarystring\n";

#list the jpg images
my(@file_list) = glob "*.jpg";
my($file) = "";

foreach $file(@file_list ) 
{
     open FILE,">", $file or die "Cannot open file $file: $!";
     print "Content-type: image/jpeg\n\n";

    while ( <FILE> )
    { 
        print "$_";
    }

    close FILE;
     print "\n--magicalboundarystring\n";
     sleep 3;
    next;

}

EDIT: added turn off i/o buffering, added "use strict" and "@file_list", "$file" are made local

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

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

发布评论

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

评论(1

桜花祭 2024-09-09 08:30:18

冲洗输出。

最有可能的是,服务器将响应保留在缓冲区中。您可能希望在每次 printautoflush STDOUT 后执行一次 fflush(STDOUT) 操作。

看看 http://www.abiglime.com/webmaster/articles/ cgi/032498.htm

[引用]

要使用下面的脚本,您需要
实现一个称为“非解析”的
您网站上的 CGI。通常情况下,网络
服务器将缓冲所有输出
你的CGI程序直到它成为程序
完成。我们不希望这种情况发生
这里。使用 Apache,这非常容易。如果
你的CGI程序的名称开始
对于“nph-”,它不会被解析。还,
将 glob“/some/path/*”更改为
您要查找文件的路径。

[/引用]

Flush the output.

Most probably, the server is keeping the response in the buffer. You may want to do fflush(STDOUT) after every print or autoflush STDOUT once.

Have a look at http://www.abiglime.com/webmaster/articles/cgi/032498.htm

[quote]

To use the script below, you'll need
to implement a called "non-parsed"
CGIs on your site. Normally, the web
server will buffer all output from
your CGI program until it the program
finishes. We don't want that to happen
here. With Apache, it's quite easy. If
the name of your CGI program starts
with "nph-" it won't be parsed. Also,
change the glob "/some/path/*" to the
path where you want to look for files.

[/quote]

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