Displaying output of command line over HTTP in real time
joel at fysh.org
Mon Dec 19 13:52:50 GMT 2005
On Mon, Dec 19, 2005 at 01:08:39PM +0000, James Davis wrote:
> I've got a command line program that I wish to launch from a mod_perl
> script. The program can take it's time to execute and the partial output
> is useful so I'd like to be able to display the output piped through
> HTTP as and when it becomes available. I'm sure I've seen this done
> before :-)
> What's the magic I need?
You have two options:
1) Keep the HTTP connection open, and keep writing new data into it.
2) Update the browser with a new page regularly.
Unless you need serious interactivity I'd suggest reading into a buffer,
and flushing that buffer into your response every 1s or so. There may be
a better/more modern way but I would use an NPH [non-parsed headers]
script so that you can instruct the browser to expect refreshing
content. Each time you have new content to send, you separate it from
the previous set of content with a pre-determined boundary string, then
send a new set of HTTP headers and content.
At the top of your perl program, add:
local $| = 1;
And to your apache configuration for that Location, add:
Then you can send your own HTTP headers, eg using CGI.pm:
use CGI qw(:standard);
my $BOUNDARY = "ARandomStringWhichWontOccurInYourContent";
-nph => 1,
-pragma => "no-cache",
-expires => '+0d',
-type => "multipart/x-mixed-replace;boundary=$BOUNDARY",
And then, when your buffer is full enough, send the content followed by
Does that give you enough information to work from?
More information about the london.pm