LWP seems to hang

Discussion in 'Perl Misc' started by masmith27, Aug 9, 2006.

  1. masmith27

    masmith27 Guest

    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;
     
    masmith27, Aug 9, 2006
    #1
    1. Advertising

  2. masmith27

    John Bokma Guest

    "masmith27" <> wrote:

    > #!/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.

    --
    John Bokma Freelance software developer
    &
    Experienced Perl programmer: http://castleamber.com/
     
    John Bokma, Aug 9, 2006
    #2
    1. Advertising

  3. masmith27

    masmith27 Guest

    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.


    John Bokma wrote:
    > "masmith27" <> wrote:
    >
    > > #!/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.
    >
    > --
    > John Bokma Freelance software developer
    > &
    > Experienced Perl programmer: http://castleamber.com/
     
    masmith27, Aug 10, 2006
    #3
  4. masmith27

    J. Gleixner Guest

    masmith27 wrote:

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


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

    That could have been a typo, which could possibly mean that you're
    leaving out other code.
     
    J. Gleixner, Aug 10, 2006
    #4
  5. masmith27

    John Bokma Guest

    "masmith27" <> wrote:

    > 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.


    --
    John Bokma Freelance software developer
    &
    Experienced Perl programmer: http://castleamber.com/
     
    John Bokma, Aug 10, 2006
    #5
  6. masmith27

    masmith27 Guest

    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.
     
    masmith27, Aug 14, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Socket Hang

    , Aug 26, 2003, in forum: Perl
    Replies:
    0
    Views:
    2,054
  2. Craig Pearson

    IIS/ASP.NET Hang

    Craig Pearson, Nov 23, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    441
    Natty Gur
    Nov 24, 2003
  3. Dag Sunde
    Replies:
    0
    Views:
    852
    Dag Sunde
    Dec 3, 2003
  4. kl
    Replies:
    0
    Views:
    899
  5. Tomek

    LWP::UserAgent infinite hang

    Tomek, Mar 5, 2007, in forum: Perl Misc
    Replies:
    1
    Views:
    157
    Mumia W.
    Mar 6, 2007
Loading...

Share This Page