加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 21:17:05 所属栏目:大数据 来源:网络整理
导读:在Perl中从另一个HTTP资源流式传输的最简单的方法是什么(没有打开shell来卷曲和从stdin读取)?我在这里假设我正在读取的HTTP资源是一个潜在的无限流(或者只是真的,非常长) 解决方法 HTTP::Lite的请求方法允许您指定回调. The $data_callback parameter,if us
在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$$datan===n";
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读