LWP seems to hang

M

masmith27

Hello, I have made a very simple script in perl that uses LWP to visit
a few local webpages. The script has to execute without any user
interaction after the computer has finished booting. When the computer
boots up it starts the script and tries to get the first webpage. The
script then seems to hang after making the request, and setting the
timeout has no effect. The strange part is that if I hit any key a few
times the script then gets the webpage and continues normally.
LWP::Debug gives the following:
LWP::UserAgent::new: ()
LWP::UserAgent::request: ()
HTTP::Cookies::add_cookie_header: Checking 127.0.0.1 for cookies
HTTP::Cookies::add_cookie_header: Checking .0.0.1 for cookies
HTTP::Cookies::add_cookie_header: Checking 0.0.1 for cookies
HTTP::Cookies::add_cookie_header: Checking .0.1 for cookies
HTTP::Cookies::add_cookie_header: Checking 0.1 for cookies
HTTP::Cookies::add_cookie_header: Checking .1 for cookies
LWP::UserAgent::send_request: GET https://127.0.0.1:2381/webpage.php
LWP::UserAgent::need_proxy: Not proxied
LWP::protocol::http::request: ()

and that's where it stops unless I hit a key or two on the keyboard.
This makes me think that for some reason perl is waiting for something
from stdin but there is nothing I can see that would make this the
case. The script works fine after hitting a few keys or after logging
into the system and issuing a command to run the script, but I need it
to run automatically using init.d. Is there a fix for something like
this? Or something I should check? I'm running out of ideas. The
relevant portion of the code is given below.

#!/usr/bin/perl -w

use LWP;
use HTTP::Cookies;

my $browser = LWP::UserAgent->new;

$browser->timeout("15");

$browser->cookie_jar( HTTP::Cookies->new('file' => '/tmp/cookies.lwp',
'autosave' => 1,
));

push @{ $browser->requests_redirectable }, 'POST';

$url = 'https://127.0.0.1:2381/webpage.php
$response = $browser->get($url);
die "Can't get $url -- ", $response->status_line unless
$response->is_success;
 
J

John Bokma

masmith27 said:
#!/usr/bin/perl -w

remove the -w

add:

use strict;
use warnings;
use LWP;
use HTTP::Cookies;

my $browser = LWP::UserAgent->new;

$browser->timeout("15");

15 is a number, why do you use "" ?
$browser->cookie_jar( HTTP::Cookies->new('file' => '/tmp/cookies.lwp',
'autosave' => 1,
));

note that the '' around file and autosave are not needed thanks to some
magic => does to the LHS.
push @{ $browser->requests_redirectable }, 'POST';

$url = 'https://127.0.0.1:2381/webpage.php

my $url = ....
$response = $browser->get($url);

my $response = ...
die "Can't get $url -- ", $response->status_line unless
$response->is_success;

I prefer:

$response->is_success or
die " ...... ";

Which I read as: for the next steps it's important that the response is
successfull, otherwise we die.


The "15" might be the cause of your timeout not working.

Also note that you need support for https installed (Crypt::SSLeay for
example) but I assume you have that installed otherwise LWP would
complain.
 
M

masmith27

Hello,
Thanks for the response. I have made the changes you suggested, but
still no change. You have reminded me that I forgot to mention a couple
things in my first post though. When I said the timeout has no response
I meant that including the timeout statement does not change the script
at all, changing the number inside has no change either, 15 was just
the last value I had tried. I also forgot to mention that I have tried
getting this website with SSL enabled and with SSL disabled since the
website does not require SSL but I have left it enabled due to some
details of the project I am working on. SSLeay is also installed.
 
J

John Bokma

masmith27 said:
Hello,
Thanks for the response. I have made the changes you suggested, but
still no change. You have reminded me that I forgot to mention a couple
things in my first post though. When I said the timeout has no response
I meant that including the timeout statement does not change the script
at all, changing the number inside has no change either, 15 was just
the last value I had tried.

I suggested that "15" is not a number, but a string. I couldn't be
bothered to check if LWP is able to use is as a number, but I do recommend
not to quote numbers if you need it as a number.

You might want to try an actual number.


Another huge tip:

http://johnbokma.com/mexit/2006/04/11/how-to-reply.html

It's not finished (the debunking part that is), but I think the recipe is
easy to follow and very sound.
 
M

masmith27

Hello again,
After looking at the problem some more I noticed it has nothing to do
with perl or lwp. The same error occurs if I try to use wget to fetch a
webpage. Also the script works fine if the pages are hosted in apache
but unfortunatly I can't use apache for reasons specific to the
project. I should have thought to test that earlier but I didn't think
of it. I have started a new topic in comp.os.linux.misc. Thanks for
the responses with this one though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top