niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » Perl
CGI-Timeout causing "internal server error"????
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
Author Message
Carl Lafferty
*nix forums beginner


Joined: 16 Jul 2006
Posts: 10

PostPosted: Thu Jul 20, 2006 8:58 pm    Post subject: CGI-Timeout causing "internal server error"???? Reply with quote

I have posted before and received a LOT of help on a project for my
public library. Right now I am having an issue using that information.

I was able to get my net::telnet application to work. it returns the
information that I want and I am in the process of converting it to a
usable script but I have run into a problem.

When the code executes I have the CGI display a form asking for a title
to search for and give them a submit button. say you enter "Tom Swift".
my script is then called again with that parameter, spends a few seconds
talking to the telnet server I have to talk to and then returns my data.
This works PERFECTLY (well some warnings but I will work those out
later) from the commandline. (I simulate the 'get something from the
script' code to run from cmd line) When I run it from the browser, it
simply gives me "internal server error" and my
/var/log/apache2/error.log says Premature end of script headers.

The code generated from the command line looks like this:
_________________
Content-type: text/html

<html><body bgcolor=blue text=white>Results:<br> <br>
Tom Swift and his aquatom Appleton, Victor J APPLETON
|ocm03632157|1 <br>
Tom Swift and his atomic Appleton, Victor J APPLETON
|ocm02915922|1 <br>
Tom Swift and his deep-se Appleton, Victor J APPLETON |
58014649|1 <br>
Tom Swift and his electro Appleton, Victor J APPLETON
|ocm01088658|1 <br>
Tom Swift and his flying Appleton, Victor J APPLETON |
54000408|1 <br>
Tom Swift and his repelat Appleton, Victor J APPLETON
|CNV02382170|1 <br>
Tom Swift and his space s Appleton, Victor J APPLETON
|ocm01088653|1 <br>
Tom Swift and his ultraso Appleton, Victor J APPLETON |
57004843|1 <br>
Tom Swift in the caves of Appleton, Victor J APPLETON
|ocm02635857|1 <br>
Tom Swift on the phantom Appleton, Victor J APPLETON
|ocm04678341|1 <br>
</body></html>

_____________________________
My question is that since it takes a 2-4 seconds to retrieve the data
(there is a delay as the telnet server accpets the login that is beyond
my control at all), could this be causing the browser to timeout????

Below is my code for my first real try into this as a cgi script.

Any help is appreciated. (



______________________________
#!/usr/bin/perl

use Net::Telnet;
#use warnings;
use CGI;


sub connect
{
#
# setup the connection to the server.
#
$galaxy = new Net::Telnet (Host => "10.10.20.220",
Port => 2001,
Timeout => 10,
Dump_Log => 'dump.log',
output_record_separator => "\x0d",
Input_log => 'input.log',
Option_log => 'option.log',
Output_log => 'output.log',
Prompt => '//',
Telnetmode => '0');


eval {
$ok = $galaxy->waitfor('/option:/');
$ok = $galaxy->print(">>>G");
$ok = $galaxy->waitfor('/option:/');
$ok = $galaxy->print("\x1c\xc0\xc1\xc0\xa7\xa8");
$ok = $galaxy->waitfor('/\x1e\x59/');
$ok = $galaxy->print("0001 AUTO||O|1.11 (800)");
$ok = $galaxy->waitfor('/YN|3/');
$ok = $galaxy->print("5000 5166 30");
$ok = $galaxy->waitfor('/0001 00000 /');
$ok = $galaxy->print("0020 ");
$ok = $galaxy->waitfor('/ile/');
$ok = $galaxy->print("0021 ");
$ok = $galaxy->waitfor('/NNNNNNNNNNN/');
$ok = $galaxy->print("0022 ");
$ok = $galaxy->waitfor('/NNNNNNNNNN/');
$ok = $galaxy->print("5000 5170 00 00 L ");
$ok = $galaxy->waitfor('/0004 000\x8f/');
$ok = $galaxy->print("5000 5163 00 00 01 ");
$ok = $galaxy->waitfor('/0001 00000/');
};
if ( $@ ) {print qq! <h1 align=center>Temporary Error</h1> <h3
align=center>A timeout with the server has occured, this page will be
refreshed in a few seconds</h3> !;}

}

sub logout
{
$galaxy->print("999");
$galaxy->print("0005 GALAXY||20");
$galaxy->print("0010");
$galaxy->close;
# print "</html>";

} #logout








sub displaymenu
{
print qq!
<form action="http://splinter.fclib.org/cgi-bin/newbooks.cgi" method="GET">
Enter title you wish to search for <input type="text" name="lookfor"
width="30"><br>
<input type="submit" value="Click to search">
</form>

!
}


sub displayheader
{

print qq!
Content-type: text/html


<html>
<head><title>Booklookup First Trial</title></head>
<body bgcolor="blue" text="white">
<h1 align="center">Book lookup trial</h1>
!
}



sub getsearchresults
{
&connect();
#put checks here to take into account different search types later.

$galaxy->print("5000 5002 30 0 $lookfor");
#get the first thing sent back which will look like " xxx xxxx xxx"
hence the
#funny looking split below to take care of the leading space.. all I am
#interested in is the $howmany variable
($temp) = $galaxy->waitfor("/\x8f/");
($junk, $howmany, $junk, $junk) = split (/ /,$temp);

#Initailly I am only going to worry about the first 10 hits since
#most of the hits you look for will be in the fist 10 but I am going to
include the
#loop that I need to do more than that incase I get ... loopy.
#gonna use parallel arrays initally here.. may use hashes later on
#
$main = 1;
@titles = ();
@cards = ();
@available = ();
while ($main < 2) {
$count = 1;
while ($count <= $howmany-1) {
($info) = $galaxy->waitfor("/\x8f/");
($controlnum) = $galaxy->waitfor("/\x8f/");
($hilite) = $galaxy->waitfor("/\x8f/");
$titles[$count] = $info . "|" . $controlnum . "|" . $hilite;
$count = $count + 1;
} #while $count
#clear out buffer
$galaxy->waitfor("/\x8f/");
$galaxy->waitfor("/\x8f/");
$main = $main + 1;
}



} #getsearchresults



sub displaysearchresults
{
print "Content-type: text/html\n\n";
print qq!<html><body bgcolor=blue text=white>!;

print qq!Results:<br>!;

$line = "";
foreach $line (@titles) { print "$line <br>\n"; }


} #displaysearchresults


##################main


$formdata = new CGI();
#print "Content-type: text/html\n\n";

#since I am only doing TITLE right now I am not going to
#worry about the search type for NOW
#
$lookfor = $formdata->param("lookfor");

$lookfor = "Tom Swift";
#$lookfor = <STDIN>;

if ($lookfor eq "") {
print "Content-type: text/html\n\n";
&displaymenu();
} else {
&getsearchresults();
&displaysearchresults();
print qq!</body></html>!;
&logout;
};
Back to top
usenet@DavidFilmer.com
*nix forums Guru


Joined: 25 Jul 2005
Posts: 545

PostPosted: Thu Jul 20, 2006 10:11 pm    Post subject: Re: CGI-Timeout causing "internal server error"???? Reply with quote

Carl Lafferty wrote:
Quote:
script' code to run from cmd line) When I run it from the browser, it
simply gives me "internal server error" and my

Put this at the top of your program:
CGI::Carp qw {carpout fatalsToBrowser warningsToBrowser };

If your script throws warnings in a shell then it can confuse your
webserver (because the warnings aren't in the form of a properly
formatted HTML page).

FWIW, any time a program works from a shell but not from a browser, the
first thing I look at is whether the userid which the webserver runs
under has access to the files, etc. A quick check is to 'su' to your
webserver userid (if you are able) and run the script from a shell
under that userid.

--
David Filmer (http://DavidFilmer.com)
Back to top
Ben Morrow
*nix forums Guru Wannabe


Joined: 24 Apr 2006
Posts: 193

PostPosted: Thu Jul 20, 2006 11:31 pm    Post subject: Re: CGI-Timeout causing "internal server error"???? Reply with quote

Quoth Carl Lafferty <laff7430@bellsouth.net>:
Quote:
I have posted before and received a LOT of help on a project for my
public library. Right now I am having an issue using that information.

I was able to get my net::telnet application to work. it returns the
information that I want and I am in the process of converting it to a
usable script but I have run into a problem.

When the code executes I have the CGI display a form asking for a title
to search for and give them a submit button. say you enter "Tom Swift".
my script is then called again with that parameter, spends a few seconds
talking to the telnet server I have to talk to and then returns my data.
This works PERFECTLY (well some warnings but I will work those out
later) from the commandline. (I simulate the 'get something from the
script' code to run from cmd line)

If you use CGI.pm (which you do) you can run the script from the
command-line and CGI.pm will deal with faking the CGI environment for
you. Then you can be *sure* you haven't messed anything else up while
converting. See CGI's docs.

Ben

--
Joy and Woe are woven fine,
A Clothing for the Soul divine William Blake
Under every grief and pine 'Auguries of Innocence'
Runs a joy with silken twine. benmorrow@tiscali.co.uk
Back to top
Carl Lafferty
*nix forums beginner


Joined: 16 Jul 2006
Posts: 10

PostPosted: Fri Jul 21, 2006 1:57 am    Post subject: Re: CGI-Timeout causing "internal server error"???? Reply with quote

Quote:
Put this at the top of your program:
CGI::Carp qw {carpout fatalsToBrowser warningsToBrowser };
I tried this and I get some weird messages back from the web server.

this one in particular
newbooks.cgi: Can't locate object method "new" via package "CGI" at
../newbooks.cgi line 153.

the line it refers to is

$formdata = new CGI;

Quote:
FWIW, any time a program works from a shell but not from a browser, the
first thing I look at is whether the userid which the webserver runs
under has access to the files, etc. A quick check is to 'su' to your
webserver userid (if you are able) and run the script from a shell
under that userid.

Now that is a good question. while I am not accessing any files I AM

accessing a telnet server.

I'll attempt it as a web user from the cmd line.
Back to top
Paul Lalli
*nix forums Guru


Joined: 10 Jun 2005
Posts: 1089

PostPosted: Fri Jul 21, 2006 3:03 am    Post subject: Re: CGI-Timeout causing "internal server error"???? Reply with quote

Carl Lafferty wrote:
Quote:
Put this at the top of your program:
CGI::Carp qw {carpout fatalsToBrowser warningsToBrowser };
I tried this and I get some weird messages back from the web server.
this one in particular
newbooks.cgi: Can't locate object method "new" via package "CGI" at
./newbooks.cgi line 153.


David goofed. It should have been:
use CGI::Carp qw/carpout fatalsToBrowswer warningsToBrowser/;
^^^^

Note the "use".

I don't know whether or not that's the *only* problem in your script,
but it's certainly one of them.

Quote:
the line it refers to is

$formdata = new CGI;

Note also that this line is not a replacement for the
use CGI;
line. It is an addition.

Paul Lalli
Back to top
usenet@DavidFilmer.com
*nix forums Guru


Joined: 25 Jul 2005
Posts: 545

PostPosted: Fri Jul 21, 2006 7:32 am    Post subject: Re: CGI-Timeout causing "internal server error"???? Reply with quote

Carl Lafferty wrote:
Quote:
newbooks.cgi: Can't locate object method "new" via package "CGI" at
./newbooks.cgi line 153.

Great - something you can Google!

Are you sure you have something like this in your code:

use CGI;

--
David Filmer (http://DavidFilmer.com)
Back to top
Dave Weaver
*nix forums addict


Joined: 22 Apr 2005
Posts: 71

PostPosted: Fri Jul 21, 2006 8:19 am    Post subject: Re: CGI-Timeout causing "internal server error"???? Reply with quote

Carl Lafferty <laff7430@bellsouth.net> wrote:

Not explicity related to your problem, but some things you should
consider...


Quote:
#!/usr/bin/perl

use Net::Telnet;
#use warnings;

Don't comment this line out - with it perl will give you warnings when
things are wrong. If you don't like the warnings, fix the relevant
line of code, don't disable the warnings. If your smoke alarm started
beeping would you look for the smoke, or unplug the alarm?

Also, add "use strict;" and then declare all your variables using "my"
at the appropriate point. This will prevent you making spelling errors
in variable names, and force you to think about the scope of your
variables, increasing the robustness of your code.

See "Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html

....
Quote:
sub getsearchresults
{
&connect();

The "&" on the subroutine call has a special meaning - if you do not
know the meaning or require its affects, you should leave off the "&".

connect();

....
Quote:
$main = 1;
@titles = ();
@cards = ();
@available = ();
while ($main < 2) {

If you only want to loop X amount of times, better to use a for()
looop. For example, to loop six times:
for my $main ( 1 .. 6 ) {

And if you're not even using $main for anything (other than the loop
count) you could use:
for ( 1 .. 6 ) {


Quote:
$count = 1;
while ($count <= $howmany-1) {

Again, you're simply looping for a set amount of iterations:

for my $count ( 1 .. $howmany-1 ) {

Quote:
($info) = $galaxy->waitfor("/\x8f/");
($controlnum) = $galaxy->waitfor("/\x8f/");
($hilite) = $galaxy->waitfor("/\x8f/");
$titles[$count] = $info . "|" . $controlnum . "|" . $hilite;

To add an item to an array, just use 'push' - then there's no need to
keep a track of the end index;

push @titles, $info . "|" . $controlnum . "|" . $hilite;

Quote:
$count = $count + 1;

This is no longer needed if you use the for() loop.

Quote:
} #while $count
#clear out buffer
$galaxy->waitfor("/\x8f/");
$galaxy->waitfor("/\x8f/");
$main = $main + 1;

No longer needed if you use the for() loop.

Quote:
}

So your code becomes:

my @titles;
for ( 1 .. 2 ) { # this is one more iteration than you had
# included here for illustrative purposes

for my $count ( 1 .. $howmany-1 ) {

# Notice that aligning the "=" on the lines below
# makes the code more readable.

($info) = $galaxy->waitfor("/\x8f/");
($controlnum) = $galaxy->waitfor("/\x8f/");
($hilite) = $galaxy->waitfor("/\x8f/");

push @titles, $info . "|" . $controlnum . "|" . $hilite;
}
# Clear out buffer
$galaxy->waitfor("/\x8f/");
$galaxy->waitfor("/\x8f/");
}

....
Quote:
sub displaysearchresults
{
....
$line = "";
foreach $line (@titles) { print "$line <br>\n"; }

Better written as:
foreach my $line (@titles) {
print "$line <br>\n";
}

Or even better:
print "$_ <br>\n" for @titles;
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
The time now is Sun Nov 23, 2008 1:57 pm | All times are GMT
navigation Forum index » Programming » Perl
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Postfix + MySQL error: very strange variable %s iWarior Postfix 0 Mon Aug 25, 2008 2:01 pm
No new posts ** Postfix error on console every minute or so ** ?? drywash Postfix 0 Fri Jul 04, 2008 8:49 pm
No new posts random timeout / delivery temporarily suspended unarcher Postfix 0 Fri Jun 13, 2008 12:33 pm
No new posts Transfer qmail email account to postfix server tallman Postfix 0 Thu Jun 05, 2008 12:43 pm
No new posts Filtering internal mail and forwarding to another domain fmaa1 Postfix 0 Wed Apr 30, 2008 12:50 pm

0 Credit Cards | Loans | Personal Finance | Problem Mortgage | Loans
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2547s ][ Queries: 16 (0.1371s) ][ GZIP on - Debug on ]