http://www.perlmonks.org?node_id=1050458

itrik0487 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am using the following code to find the parse the Apache webserver server status url. It is using the IPv6 address to build the URL. But when I am trying to run the program I am getting the error:

"500 Can't connect to localhost:80 (Connection Refused) at ./sample.pl line 19"

Code that I am using is as following:

#!/root/localperl/bin/perl use strict; use Warnings; use utf8; use LWP::UserAgent; use Data::Dumper; our $URL="http://localhost"; my $ua = LWP::UserAgent->new; my $res = $ua->get($URL); if($res->is_success) { print $res->decoded-content; } else { die $res->status-line; }
  • Comment on 500 Can't connect to localhost:80 (Connection Refused) at ./sample.pl line 19
  • Download Code

Replies are listed 'Best First'.
Re: 500 Can't connect to localhost:80 (Connection Refused) at ./sample.pl line 19
by Preceptor (Deacon) on Aug 22, 2013 at 07:16 UTC

    Connection refused usually means - there's nothing running on that port. A simple way to test this, is use 'telnet':

    telnet localhost 80 GET / HTTP/1.0

    Type the 'get' bit if the telnet connects, and hit enter twice, and that's a very basic test of 'is there a webserver here'. Chances are though - you'll get 'connection refused' as well - it's not your perl code that's at fault, but the webserver. You may need to use http://<hostname> as 'localhost' is a different interface. Or check your apache config for which port number it's using

      The server is up and running and I can access it using http://<localhost> and http://<hostname> both.. program was working fine with IPv4 Address but when running it for IPv6 it is failing with Connection Refused errors.
Re: 500 Can't connect to localhost:80 (Connection Refused) at ./sample.pl line 19
by MidLifeXis (Monsignor) on Aug 22, 2013 at 13:37 UTC

    Please post the actual code you are running. Neither decoded-content or status-line are provided by LWP::UserAgent (decoded_content and status_line are) nor are even valid perl identifier names.

    --MidLifeXis

      decoded-content and status-line are provided by LWP::UserAgent. The program is working fine with IPv4 address

      but lets just keep it aside and if I want to run the program with the following code also it is failing.

      #!/usr/bin/perl require LWP::UserAgent; require HTTP::Request; use FindBin ; use Net::IP; use LWP::UserAgent; use lib $FindBin::Bin ; my $urlString; $urlString="http://[2620:0:a17:e03d:250:56ff:fea4:6d]/server-status"; #Send a get request to the above URL. my $ua; my $response; my $html_file; $ua = LWP::UserAgent->new; #print $urlString; $response = $ua->get($urlString); if (!$response->is_success) { print "Fialed to connect to the URL $urlString \n"; exit(0); }

      If I am using the URL provided in the program using mozilla firefox I am able to see the page. But the response of the same is not coming as "is_success" in the program

      Can anyone please help me to get a program to connect to the url containing IPv6 address in it

        No, neither decoded-content nor status-line are provided. The provided methods are decoded_content and status_line. Note that - is not a valid character in a name.

        According to Re^2: 500 Can't connect to localhost:80 (Connection Refused) at ./sample.pl line 19, you can access it with localhost or the hostname, but on the original post, you cannot access it with localhost. Which is it?

        Is your web server configured to accept connections on an IPv6 address? Is there any more diagnostic information you can provide? There is very little to go on here.

        --MidLifeXis

Re: 500 Can't connect to localhost:80 (Connection Refused) at ./sample.pl line 19
by Anonymous Monk on Aug 22, 2013 at 07:10 UTC

    What is your question? Why is connection refused? Thats impossible to know from perl code -- ask the bartender why he won't serve you drinks (ask webserver, firewall... -- check logs).

Re: 500 Can't connect to localhost:80 (Connection Refused) at ./sample.pl line 19
by MidLifeXis (Monsignor) on Aug 29, 2013 at 18:36 UTC

    I am going to start over with my questions, as I think I may now have a handle on what you are asking.

    • It seems to me that you are saying that your web server responds using Firefox to any IPv4 or IPv6 address which is valid for the machine.
    • It also seems that you are saying that LWP is able to handle contacting the web server using any IPv4 address for the machine.
    • It seems like you are stating that using LWP to access an IPv6 address for the server is failing, even if it is an identical URL that Firefox can use to access the server.
    Do I have that correct?

    --MidLifeXis

      yes you are right

        I got it working with the following code

        use Net::INET6Glue::INET_is_INET6; use LWP::Simple; print get( 'http://[2620:0:a17:e03d:250:56ff:fea4:6d]/server-status' +);