开发者

Perl: Value of response code in HTTP::Request

So, I am writing a code to get a document from the internet. The document size is around 200 KB. This is the code:

#!/usr/local/bin/perl -w
use strict;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $url = "SOME URL";
my $req = HTTP::Request->new(GET => $url);
my $res = $ua->request($req);

if($res->is_success){
   print $res->content ."\n";
}
else{
  print "Error: " . $res->status_line;
}

Now, the only problem is I can't mention what the URL is.

However, the开发者_C百科 output is: "Error: 500 read timeout". When I checked the link externally, the data is being downloaded in under 5 seconds.

I even changed the timeout to 1000s, but it still didn't work. How should I go about finding more information related to the response? The size of the file (around 200KB) is also not big enough to warrant a read timeout. The server is also not a busy one, didn't give a problem whenever I checked the link on the browser.

Thanks.


Make sure webserver is not configured to drop requests from scripts in this case perl.


When network applications give you trouble, debug using Wireshark.


The web server (Apache/nginx) may have a backend app written on PHP/Python/whatever. Probably that backend isn't answering anything, so the web server stop waiting (reach timeout) and answer to you on the http request a 5xx (internal server) error.

One great tool for testing is curl with -v:

curl --verbose --head "SOME URL"
  • --head to fetch only http response header.
  • If from same host on a browser everything is OK there is something in your http request header that make the difference. You can see con browser dev tool what you are sending and add them to curl with -H, --header <header>. Adjusting curl parameters web server can't tell if the received http req is from a web browser, a script, or anything else (protocols magic!).
  • Use -s (silent) and 2>&1 to send curl output through pipe to less/head/tail.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜