Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Perl / General Perl Topics / July 2009



Tip: Looking for answers? Try searching our database.

Very Frustrating

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
schnibitz - 30 Jul 2009 01:23 GMT
Hi everyone, the following code works from my linux shell as root or
the 'nobody' accounts:
-----------------------------

#!/usr/bin/perl
#use strict;
#use LWP::Simple;
use LWP::UserAgent;
#use URI::URL;

print "Content-type: text/html\n\n";
print <<HTML;
<html>
<head>
<title>TEST</title>
</head>
<body>
HTML

my $url_variable = $input{'URL'};

my $ua = new LWP::UserAgent;
$ua->timeout(120);
my $request = new HTTP::Request('GET', 'http://www.yahoo.com');
my $response = $ua->request($request);
my $content = $response->content();
print "$content";
print <<HTML;
</body>
</html>
</head>
HTML
---------------------------------

BUT when I hit that same .pl file with my browser I get the following
error:

500 Can't connect to www.yahoo.com:80 (Bad hostname 'www.yahoo.com')

Can someone help me please? I'm doing something idiotic, I know, and
anything you could do to help would be appreciated.

-G
Tad J McClellan - 30 Jul 2009 02:48 GMT
> #use strict;

You lose all of the benfits of that pragma when you comment
it out like that.

Signature

Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Tony Curtis - 30 Jul 2009 03:53 GMT
>> On Wed, 29 Jul 2009 20:48:52 -0500,
>> Tad J McClellan <tadmc@seesig.invalid> said:

>> #use strict;

> You lose all of the benfits of that pragma when you
> comment it out like that.

I've been writing perl since 1986 or so, and while
developing, I *always* enable

 use strict;
 use warnings;
 use diagnostics;

I'm good at writing perl, but I'm good enough to know I
make stupid mistakes.

hth
t
Glenn Jackman - 30 Jul 2009 11:14 GMT
>  I've been writing perl since 1986 or so, and while
>  developing, I *always* enable
>  
>    use strict;
>    use warnings;
>    use diagnostics;

And for CGI scripts,

   use CGI::Carp qw{fatalsToBrowser};

to show 'die' messages in your browser in addition to the web server
error log.

Signature

Glenn Jackman
   Write a wise saying and your name will live forever. -- Anonymous

schnibitz - 30 Jul 2009 03:53 GMT
> > #use strict;
>
[quoted text clipped - 4 lines]
> Tad McClellan
> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Thank you, I've tried both ways BTW, and it doesn't give me any more
helpful messages.  I checked DNS on my server.  I can ping the
address, and it resolves to an IP just fine.  I've checked the perl
resolver by issuing a special perl command, and it too resolves just
fine.  I can wget the site without issue, my /etc/hosts file has two
measly entries, so I don't think that a corrupt hosts file has
anything to do with it.  Why would it work command-line, but not when
invoked through the browser?
Tony Curtis - 30 Jul 2009 03:59 GMT
> Thank you, I've tried both ways BTW, and it doesn't give me any more
> helpful messages.  I checked DNS on my server.  I can ping the
[quoted text clipped - 4 lines]
> anything to do with it.  Why would it work command-line, but not when
> invoked through the browser?

maybe there's something restricted about the web server?

if it works from the command-line then there's nothing
obviously wrong with the perl logic: maybe there's
something to do with the web server config that breaks
things?

hth
t
schnibitz - 30 Jul 2009 04:56 GMT
> > Thank you, I've tried both ways BTW, and it doesn't give me any more
> > helpful messages.  I checked DNS on my server.  I can ping the
[quoted text clipped - 14 lines]
> hth
> t

Hi there, great suggestion, and I'll be doing that from now on.  I
added all of that in, and I'm still not getting any helpful errors.  I
don't understand why it works in the shell, but not when invoked from
a browser.  I know that the browser hits that file from a different
user (nobody I think) and I've tried invoking it from the "nobody"
acct BTW.

Here is the updated code with your suggested additions, and minor
clarifications:

_______________________________________
#!/usr/bin/perl
 use strict;
 use warnings;
 use diagnostics;
#use LWP::Simple;
use LWP::UserAgent;
#use URI::URL;

my $content = 0;
my $error = 0;

print "Content-type: text/html\n\n";
print <<HTML;
<html>
<head>
<title>TEST</title>
</head>
<body>
HTML
&getpage;
print "$content";
print "$error";
print <<HTML;
</body>
</html>
</head>
HTML

sub getpage {
    my $ua = new LWP::UserAgent;
    $ua->timeout(120);
    my $request = new HTTP::Request('GET', 'http://www.yahoo.com');
    my $response = $ua->request($request);
    if ($response->is_success) {
          $content = $response->content;
    }
    else {
      $error = $response->status_line, "\n";
       }
}

_____________________________________
Any other things I can try?
Tad J McClellan - 30 Jul 2009 05:13 GMT
> I
> don't understand why it works in the shell, but not when invoked from
> a browser.  I know that the browser hits that file from a different
> user (nobody I think) and I've tried invoking it from the "nobody"
> acct BTW.

If you "think" wrong, then the fact that it runs as nobody is irrelevant.

What is relevant is if it runs from the command line as the same use
that your CGI program runs as.

So the first step is to determine what user your CGI programs run as.

   #!/usr/bin/perl
   print "Content-Type: text/plain\n\n";
   system 'whoami';

> print "$content";

   perldoc -q vars

Signature

Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

schnibitz - 30 Jul 2009 06:01 GMT
> > I
> > don't understand why it works in the shell, but not when invoked from
[quoted text clipped - 20 lines]
> Tad McClellan
> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Hi Tad, fair enough, I removed the quotes.  I normally don't use them,
but I started just trying anything (yeah I'm that desperate).  Anyway,
I added the "system 'whoami';" command, and it shows "apache" instead
of "nobody".  So I su to "apache" and ran the script again.
Flawless.  Not a single error or anything thrown that I can see.
Still getting "bad hostname" error when accessed from the browser.
schnibitz - 30 Jul 2009 06:26 GMT
> > > I
> > > don't understand why it works in the shell, but not when invoked from
[quoted text clipped - 27 lines]
> Flawless.  Not a single error or anything thrown that I can see.
> Still getting "bad hostname" error when accessed from the browser.

Okay, one other quick thing . . . So when I add the IP for www.yahoo.com
into the /etc/hosts file, here's what I get when I run it from my
browser now:

0500 Can't connect to www.yahoo.com:80 (Permission denied)

Different message.  Anyone make anything of that?
Andrew DeFaria - 30 Jul 2009 15:23 GMT
schnibitz wrote:
Okay, one other quick thing . . . So when I add the IP for www.yahoo.com into the /etc/hosts file, here's what I get when I run it from my browser now:

0500 Can't connect to www.yahoo.com:80 (Permission denied)

Different message. Anyone make anything of that?
If you can su to other users and edit /etc/hosts then why didn't you tail Apache's error log for clues?
--
Andrew DeFaria
If a cow laughed, would milk come out her nose?
schnibitz - 30 Jul 2009 06:40 GMT
> > > I
> > > don't understand why it works in the shell, but not when invoked from
[quoted text clipped - 27 lines]
> Flawless.  Not a single error or anything thrown that I can see.
> Still getting "bad hostname" error when accessed from the browser.

Okay, last post I swear.  That last error got me to realize that
selinux was hurting me.  It would not allow my script to open up a
port.  Thank you to everyone for bearing with me, and hopefully this
will help some poor soul out there.
Tad J McClellan - 30 Jul 2009 15:45 GMT
>> Still getting "bad hostname" error when accessed from the browser.
>
> That last error got me to realize that
> selinux was hurting me.  It would not allow my script to open up a
> port.  

It is traditional to modify the Subject by adding

   [Solved]

So that others who are following (or searching) the thread because
they have the same problem can easily figure out which of the
many posts in the thread is the one containing the cause and solution.

But that is moot in this case, since the Subject you chose
is so useless anyway.

> Thank you to everyone for bearing with me, and hopefully this
> will help some poor soul out there.

That is not very likely, as they are not likely to think that
a thread named "Very Frustrating" will have anything to do
with a "bad hostname" message.

Please put the subject of your article in the Subject of your article.

When your mail program asks for "Subject" it is not asking
"how are you feeling right now", it is asking for what you
plan to write about.

You did not write about being frustrated, you wrote about:

   Subject: getting "bad hostname" error when accessed from the browser

Have you seen the Posting Guidelines that are posted here frequently?

Signature

Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

schnibitz - 30 Jul 2009 04:57 GMT
> > Thank you, I've tried both ways BTW, and it doesn't give me any more
> > helpful messages.  I checked DNS on my server.  I can ping the
[quoted text clipped - 14 lines]
> hth
> t

Hadn't thought of that, I'll check into that possibility.  Thank you!
Tad J McClellan - 30 Jul 2009 04:50 GMT
> I've checked the perl
> resolver

What is a perl resolver?

> by issuing a special perl command,

What special perl command is that?

> and it too resolves just
> fine.

> Why would it work command-line, but not when
> invoked through the browser?

Something in the environment is different.

  perldoc -q "runs from the command line but not the browser"

Signature

Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.