What is the easiest way in pure Perl to stream from another HTTP resource?

前端 未结 5 1242
闹比i
闹比i 2021-01-01 00:01

What is the easiest way (without opening a shell to curl and reading from stdin) in Perl to stream from another HTTP resource? I\'m assuming here that the HTTP resource I\'m

5条回答
  •  迷失自我
    2021-01-01 00:21

    Here is a version I ended up using via Net::HTTP

    This is basically a copy of the example from the Net::HTTP man page / perl doc

    use Net::HTTP;
    
    my $s = Net::HTTP->new(Host => "www.example.com") || die $@;
    $s->write_request(GET => "/somestreamingdatasource.mp3");
    my ($code, $mess, %h) = $s->read_response_headers;
    while (1) {
      my $buf;
      my $n = $s->read_entity_body($buf, 4096);
      die "read failed: $!" unless defined $n;
      last unless $n;
      print STDERR "got $n bytes\n";
      print STDOUT $buf;
    }
    

提交回复
热议问题