纯Perl从另一个HTTP资源流式传输的最简单方法是什么?

前端之家收集整理的这篇文章主要介绍了纯Perl从另一个HTTP资源流式传输的最简单方法是什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在Perl中从另一个HTTP资源流式传输的最简单的方法是什么(没有打开shell来卷曲和从stdin读取)?我在这里假设我正在读取的HTTP资源是一个潜在的无限流(或者只是真的,非常长)

解决方法

HTTP::Lite的请求方法允许您指定回调.

The $data_callback parameter,if used,is a way to filter the data as it is received or to handle large transfers. It must be a function reference,and will be passed: a reference to the instance of the http request making the callback,a reference to the current block of data about to be added to the body,and the $cbargs parameter (which may be anything). It must return either a reference to the data to add to the body of the document,or undef.

但是,查看源代码,子请求中似乎存在一个错误,它似乎忽略了传递的回调.使用set_callback似乎更安全:

#!/usr/bin/perl

use strict;
use warnings;

use HTTP::Lite;

my $http = HTTP::Lite->new;
$http->set_callback(\&process_http_stream);
$http->http11_mode(1);

$http->request('http://www.example.com/');

sub process_http_stream {
    my ($self,$phase,$dataref,$cbargs) = @_;
    warn $phase,"\n";
    return;
}

输出

C:\Temp> ht
connect
content-length
done-headers
content
content-done
data
done

看起来传递给请求方法的回调被区别对待:

#!/usr/bin/perl

use strict;
use warnings;

use HTTP::Lite;

my $http = HTTP::Lite->new;
$http->http11_mode(1);

my $count = 0;
$http->request('http://www.example.com/',\&process_http_stream,\$count,);

sub process_http_stream {
    my ($self,$data,$times) = @_;
    ++$$times;
    print "$$times====\n$$data\n===\n";
}
原文链接:https://www.f2er.com/Perl/172922.html

猜你在找的Perl相关文章