Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Inspired by LWP head mystery, where a server replies incorrectly to a HEAD request, which is apparently a common bug, this snippet makes a GET request, but closes the socket after reading only 1 character of the content, thus doing basically the same thing HEAD does, while not having to deal with the common problem of a broken HEAD reply.
#!/usr/bin/perl -w $^W = 552 >> 3; use strict; # for sanity (ALWAYS!!!) use LWP::UserAgent; use HTTP::Request; use HTTP::Response; HEAD('http://123box.co.uk/'); HEAD('http://japhy.perlmonk.org/book/'); real_HEAD('http://123box.co.uk/'); real_HEAD('http://japhy.perlmonk.org/book/'); sub HEAD { my $req = HTTP::Request->new(GET => shift); my $UA = new LWP::UserAgent; my $res = $UA->request($req, sub { die }, 1); if($res->is_success) { print $res->as_string(); } else { print "Error: " . $res->status_line . "\n"; } } sub real_HEAD { my $req = HTTP::Request->new(HEAD => shift); my $UA = new LWP::UserAgent; my $res = $UA->request($req); if($res->is_success) { print $res->as_string(); } else { print "Error: " . $res->status_line . "\n"; } } __END__ F:\dev\snippets>perl fake.HEAD.pl HTTP/1.1 200 OK Connection: close Date: Wed, 21 Nov 2001 07:49:38 GMT Server: Apache/1.3.19 (Unix) mod_gzip/1.3.19.1a Resin/1.2.2 Content-Type: text/html Client-Date: Wed, 21 Nov 2001 07:39:46 GMT Client-Peer: 212.67.197.196:80 X-Died: Died at fake.HEAD.pl line 18. HTTP/1.1 200 OK Connection: close Date: Wed, 21 Nov 2001 07:40:25 GMT Accept-Ranges: bytes Server: Apache/1.3.22 (Unix) mod_perl/1.26 PHP/4.0.6 Content-Length: 1766 Content-Type: text/html ETag: "57532-6e6-3bf6bcac" Last-Modified: Sat, 17 Nov 2001 19:38:20 GMT Client-Date: Wed, 21 Nov 2001 07:39:55 GMT Client-Peer: 66.92.212.9:80 X-Died: Died at fake.HEAD.pl line 18. Error: 500 unexpected EOF before status line seen HTTP/1.1 200 OK Connection: close Date: Wed, 21 Nov 2001 07:40:35 GMT Accept-Ranges: bytes Server: Apache/1.3.22 (Unix) mod_perl/1.26 PHP/4.0.6 Content-Length: 1766 Content-Type: text/html ETag: "57532-6e6-3bf6bcac" Last-Modified: Sat, 17 Nov 2001 19:38:20 GMT Client-Date: Wed, 21 Nov 2001 07:40:03 GMT Client-Peer: 66.92.212.9:80
If you wanna try to benchmark (which wouldn't be accurate cause it's network stuff) just add
print "\n>>>>>>>>>>>>>>>>>>>>and now for the benchmark\n"; use Benchmark; timethese(50, { 'real_HEAD' => sub { real_HEAD('http://japhy.perlmonk. +org/book/')}, 'HEAD' => sub { HEAD('http://japhy.perlmonk.org/book/' +)}, });
I got something resembling
........ output from the HEAD ..... HEAD: 140 wallclock secs (139.73 usr + 0.00 sys = 139.73 CPU) @ + 0.36/s (n=50) ........ output from the HEAD ..... real_HEAD: 208 wallclock secs (208.00 usr + 0.00 sys = 208.00 CPU) @ + 0.24/s (n=50)

In reply to LWP head replacement by crazyinsomniac

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found