PDA

View Full Version : Pass Arguments to CGI



Wojtek
February 5th, 2003, 22:08
Hey all,

I have yet another cgi problem :)

This time I need to pass arguments to the script.
How can this be done?

I need to do this:

quote.cgi -r dell



when I try via IE, it gives 404 error as it tries to load the whole file+argument as one file.



when I tried it in SSH via:

exec /home/newsave/public_html/cgi-bin/quote.cgi -r dell

It did work perfectly



Then I tried via the ssi tag exec cgi.

<!--#exec cgi="/cgi-bin/quote.cgi -r dell" -->

It didnt work [an error occured while blabla]



<!--#exec cgi="/cgi-bin/quote.cgi" -->
Works, displays defaul skript text



How can I pass the arguments "-r dell" to this script using ssi or any other method :)

Thanks

Wojtek

YUPAPA
February 5th, 2003, 23:34
HiHi~


Originally posted by Wojtek
Hey all,

I have yet another cgi problem :)

This time I need to pass arguments to the script.
How can this be done?

I need to do this:

quote.cgi -r dell



when I try via IE, it gives 404 error as it tries to load the whole file+argument as one file.



when I tried it in SSH via:

exec /home/newsave/public_html/cgi-bin/quote.cgi -r dell

It did work perfectly


Did you put:
exec /home/newsave/public_html/cgi-bin/quote.cgi -r dell

to SSH? Is this a perl script?
I think you should put:
perl /home/newsave/public_html/cgi-bin/quote.cgi

And I am not sure what the -r flags do... What does the script do? :p



Originally posted by Wojtek
Then I tried via the ssi tag exec cgi.

<!--#exec cgi="/cgi-bin/quote.cgi -r dell" -->

It didnt work [an error occured while blabla]



<!--#exec cgi="/cgi-bin/quote.cgi" -->
Works, displays defaul skript text



How can I pass the arguments "-r dell" to this script using ssi or any other method :)

Thanks

Wojtek [/B]

Try <!--#exec cmd="exec /home/newsave/public_html/cgi-bin/quote.cgi -r dell"-->
This may not work tho cos the http server (apache) may disable ssi to run command lines...

Wojtek
February 6th, 2003, 01:01
Thanks for your reply yupapa.

What the script does is that it connects to finance.yahoo.com and strips the current stock values of a given stock.

quote.cgi <- the file itself
-r <- Reaktime quote mode
dell <- was just an exemple of a quote

yes, I did put "exec /home/newsave/public_html/cgi-bin/quote.cgi -r dell" in SSH.


When I downloaded this, it told me stupid stuff about a make file or something and that I have to compile and blabla. So I just skipped that, renamed quote to quote.cgi (code inside was perl) and tried it via SSH. But I cant make it work via the web...

Here is the code if that may help you:



#!/usr/bin/perl -w

# This is Quote, version 0.05
#
# CVS revision:
# $Id: quote.in,v 1.5 2000/09/27 09:20:21 jelson Exp $

$lynx_path = "/usr/bin/lynx";


############################################################ ###############


if ($#ARGV < 0) {
print "Usage: quote [-r] <symbol> [symbol2] [symbol3] ...\n";
print "\n\n The -r option tries to get real-time prices.\n";
exit;
}

$method = "";

if ( eval { require LWP::Simple; }) {
$method = "LWP";
} elsif ($lynx_path ne "") {
$method = $lynx_path . " -source";
}

if ($method eq "") {
print "You need to install the Perl LWP module, or lynx.\n";
exit;
}

# get the list of symbols the user wants
$symlist = "";
$realtime = 0;
while ($next_sym = shift) {
if ($next_sym eq "-r") {
$realtime = 1;
next;
}
$symlist = $symlist . "$next_sym+";
}
# ...and take off the last "+"
chop($symlist);


# decide which format string to use, depending on if the user
# requested realtime quotes or not. in hacking around with yahoo's
# "quotes.csv" script that generates spreadsheet-formatted quotes, i
# think the format string is decoded as follows:
#
# s = symbol
# v = volume
# g = day low
# h = day high
# o = open
#
# c1 = change formatted as "+1.25" or "-0.6875"
# t1 = time of last trade
# d1 = date of last trade
#
# c6, t5 = change and time but with ECN realtime data

if ($realtime) {
$format = "sl9c6t5d1v";
} else {
$format = "sl1c1t1d1v";
}

$url = "http://finance.yahoo.com/d/quotes.csv?s=" . $symlist .
"&f=$format&e=.csv";

# go get it!
if ($method eq "LWP") {
$QUOTE = LWP::Simple::get($url);
@QUOTE = split(/\n/, $QUOTE);
} else {
open(PIPE, "$method '$url'|");
@QUOTE = <PIPE>;
close PIPE;
}


sub strip_quotes ($) {
my ($s) = @_;
$s =~ s/\"//g;
return $s;
}

foreach $_ (@QUOTE) {
@cols = split(/,/);

$curr_sym = strip_quotes($cols[0]);
$price = $cols[1];
$change = strip_quotes($cols[2]);
$time = strip_quotes($cols[3]);
$date = strip_quotes($cols[4]);
$vol = $cols[5];

next if $price == 0 || $date eq "N/A";

# Compute the percent change
$pctchange = (100.0 * $change) / ($price - $change);

# Format the "volume" column
if ($vol !~ /^[\d]+/) {
$vol = 0;
} else {
$vol_suffix = "";
if ($vol >= 1000) { $vol /= 1000; $vol_suffix = "K"; }
if ($vol >= 1000) { $vol /= 1000; $vol_suffix = "M"; }
if ($vol >= 1000) { $vol /= 1000; $vol_suffix = "B"; }
}

printf "%s: %.2f %+.2f (%+.2f%%)", $curr_sym, $price, $change, $pctchange;
if (!$realtime && $vol > 0) {
printf " on vol of %.1f%s", $vol, $vol_suffix;
}
printf " \@$time\n";
}


here's some info from the readme:

DESCRIPTION

quote is a very short and simple Perl script that retrieves
stock quotes from Yahoo! Finance (http://finance.yahoo.com).
It's a clean and fast command-line interface for getting
snapshot stock market prices. quote can be used to retrieve
prices of U.S. stocks (CSCO, DELL), non-U.S. stocks (NT.TO),
and market indices (^DJI, ^IXIC). Note that the ^ character is
a shell meta-character and may need to be escaped or quoted.

quote uses the Perl LWP module if available, or lynx otherwise
(found in /usr/bin/lynx).

OPTIONS
-r Attempt to retrieve real-time quotes from Yahoo!
Finance. This is not supported for all stock symbols.

EXAMPLES
Use of quote is very simple:

% quote csco ibm dell nt.to '^ixic'
CSCO: 55.19 -2.00 (-3.50%) on vol of 72.6M @4:00PM
IBM: 119.12 -3.88 (-3.15%) on vol of 6.2M @4:52PM
DELL: 33.62 -0.69 (-2.00%) on vol of 35.2M @4:00PM
NT.TO: 95.50 -4.10 (-4.12%) on vol of 6.3M @4:58PM
^IXIC: 3689.10 -52.12 (-1.39%) @5:16PM

You can also try to retrieve real-time quotes:

% quote -r csco dell
CSCO: 55.62 -1.56 (-2.73%) @7:58 PM
DELL: 33.88 -0.44 (-1.28%) @7:39 PM

YUPAPA
February 6th, 2003, 01:41
This thing only run in command-line interface... so i guess you get internal server error when running the script from the web browser since I don't see the script send any headers...

BUT I can try to modify it...

Does this work then?
<!--#exec cmd="exec /home/newsave/public_html/cgi-bin/quote.cgi -r dell"-->

this tells SSI to run the command:
exec /home/newsave/public_html/cgi-bin/quote.cgi -r dell

If you use SSI to run the command above, the script doesn't need to send any headers

Wojtek
February 6th, 2003, 01:48
Yes, I've tried, but it dosnt work.

Yupapa,

Could you give me a bit of code that does this:

quote.cgi?realtime=0&symlist=dell

that 0 is saved as $realtime
and dell saved in $symlist


Basically saves the parsed things as variables in the script.

I've added
print "Content-type: text/html\n\n";
to the top of the script, and now when I hardcode $symlist="dell" it does work when calling newsave.net/cgi-bin/quote.cgi

Now if only that parsing thing would work it would be great :)

YUPAPA
February 6th, 2003, 11:48
oh wow, that makes my life easier~ :smile2:
Can you attach your working code to me so that I can work out quote.cgi?realtime=0&symlist=dell for you?

Wojtek
February 6th, 2003, 11:56
Here it is,
I commented where exactly the thing is located :)

Thanks yupapa


#!/usr/bin/perl -w

$lynx_path = "/usr/bin/lynx";


##########################################

print "Content-type: text/html\n\n";

$method = "";


if ( eval { require LWP::Simple; }) {
$method = "LWP";
} elsif ($lynx_path ne "") {
$method = $lynx_path . " -source";
}

if ($method eq "") {
print "You need to install the Perl LWP module, or lynx.\n";
exit;
}



#
# quote.cgi?realtime=0&symlist=dell
#
# Goes here:
#
# realtime=0 saved as $realtime
#
# symlist=dell saved as $symlist
#

$symlist = "";
$realtime = 0;

#
#
#

while ($next_sym = shift) {
if ($next_sym eq "-r") {
$realtime = 1;
next;
}
$symlist = $symlist . "$next_sym+";
}

chop($symlist);


if ($realtime) {
$format = "sl9c6t5d1v";
} else {
$format = "sl1c1t1d1v";
}

$url = "http://finance.yahoo.com/d/quotes.csv?s=" . $symlist .
"&f=$format&e=.csv";

if ($method eq "LWP") {
$QUOTE = LWP::Simple::get($url);
@QUOTE = split(/\n/, $QUOTE);
} else {
open(PIPE, "$method '$url'|");
@QUOTE = <PIPE>;
close PIPE;
}


sub strip_quotes ($) {
my ($s) = @_;
$s =~ s/\"//g;
return $s;
}

foreach $_ (@QUOTE) {
@cols = split(/,/);

$curr_sym = strip_quotes($cols[0]);
$price = $cols[1];
$change = strip_quotes($cols[2]);
$time = strip_quotes($cols[3]);
$date = strip_quotes($cols[4]);
$vol = $cols[5];

next if $price == 0 || $date eq "N/A";

# Compute the percent change
$pctchange = (100.0 * $change) / ($price - $change);

# Format the "volume" column
if ($vol !~ /^[\d]+/) {
$vol = 0;
} else {
$vol_suffix = "";
if ($vol >= 1000) { $vol /= 1000; $vol_suffix = "K"; }
if ($vol >= 1000) { $vol /= 1000; $vol_suffix = "M"; }
if ($vol >= 1000) { $vol /= 1000; $vol_suffix = "B"; }
}

printf "%s: %.2f %+.2f (%+.2f%%)", $curr_sym, $price, $change, $pctchange;
if (!$realtime && $vol > 0) {
printf " on vol of %.1f%s", $vol, $vol_suffix;
}
printf " \@$time\n";
}

YUPAPA
February 6th, 2003, 12:11
Call it like this:
script.pl?symlist=dell&realtime=0



#!/usr/bin/perl -w
use CGI qw(:standard :form);

$query = new CGI();
$lynx_path = "/usr/bin/lynx";

##########################################

print "Content-type: text/html\n\n";

$method = "";


if ( eval { require LWP::Simple; }) {
$method = "LWP";
} elsif ($lynx_path ne "") {
$method = $lynx_path . " -source";
}

if ($method eq "") {
print "You need to install the Perl LWP module, or lynx.\n";
exit;
}


#
# quote.cgi?realtime=0&symlist=dell
#
# Goes here:
#
# realtime=0 saved as $realtime
#
# symlist=dell saved as $symlist
#

$symlist = $query->param('symlist');
$realtime = $query->param('realtime');

#
#
#

while ($next_sym = shift) {
if ($next_sym eq "-r") {
$realtime = 1;
next;
}
$symlist = $symlist . "$next_sym+";
}

chop($symlist);


if ($realtime) {
$format = "sl9c6t5d1v";
} else {
$format = "sl1c1t1d1v";
}

$url = "http://finance.yahoo.com/d/quotes.csv?s=" . $symlist .
"&f=$format&e=.csv";

if ($method eq "LWP") {
$QUOTE = LWP::Simple::get($url);
@QUOTE = split(/n/, $QUOTE);
} else {
open(PIPE, "$method '$url'|");
@QUOTE = <PIPE>;
close PIPE;
}


sub strip_quotes ($) {
my ($s) = @_;
$s =~ s/"//g;
return $s;
}

foreach $_ (@QUOTE) {
@cols = split(/,/);

$curr_sym = strip_quotes($cols[0]);
$price = $cols[1];
$change = strip_quotes($cols[2]);
$time = strip_quotes($cols[3]);
$date = strip_quotes($cols[4]);
$vol = $cols[5];

next if $price == 0 || $date eq "N/A";

# Compute the percent change
$pctchange = (100.0 * $change) / ($price - $change);

# Format the "volume" column
if ($vol !~ /^[d]+/) {
$vol = 0;
} else {
$vol_suffix = "";
if ($vol >= 1000) { $vol /= 1000; $vol_suffix = "K"; }
if ($vol >= 1000) { $vol /= 1000; $vol_suffix = "M"; }
if ($vol >= 1000) { $vol /= 1000; $vol_suffix = "B"; }
}

printf "%s: %.2f %+.2f (%+.2f%%)", $curr_sym, $price, $change, $pctchange;
if (!$realtime && $vol > 0) {
printf " on vol of %.1f%s", $vol, $vol_suffix;
}
printf " @$timen";
}

Wojtek
February 6th, 2003, 12:38
Yupapa!

Your GOD :biggrin2:

Tnx alot!

YUPAPA
February 6th, 2003, 15:43
Does it work then? :smile2:
what about the SSI? :p

can you call it like this:
<!--#exec cgi="/cgi-bin/quote.cgi?symlist=dell&realtime=0"-->

Wojtek
February 6th, 2003, 16:36
Originally posted by YUPAPA
Does it work then? :smile2:
what about the SSI? :p

can you call it like this:
<!--#exec cgi="/cgi-bin/quote.cgi?symlist=dell&realtime=0"--> ]
Yes it does work.
I need to use include virtual instead of exec cgi. don't know why.
All I know is that it works! :classic2:

Thanks again yupapa!