Calling cgi from cgi thru 'system' function. Different behaviour on browser v/s cmd line

Discussion in 'Perl' started by Shailan, Dec 12, 2003.

  1. Shailan

    Shailan Guest

    Hi

    Im having trouble with the following code that seems to be behave
    differently when called from the browser as opposed to the command
    line. The calling script is a cgi that forks, with the child trying to
    call another cgi script and pass arguments to it. It works fine from
    the command line, and calls the required script and passes the
    arguments correctly. However, when it is run on the browser, it calls
    the script but does not pass the arguments to it.
    The called script (included) is also a cgi, set up to accept
    parameters via the param function. It accepts the parameters when the
    calling script is run on the command line, but not when the calling
    script is run on the browser.

    I have also tried putting a '?' between the script name and the
    arguments to get the "script.cgi?argument=1" format, and that didnt
    work either.

    Im not sure if the problem is in the system call, the use of CGI in
    the called script, or somewhere else entirely. Any suggestions would
    be extremely helpful...

    Thanks in advance
    Shailan

    #### Calling script

    if (my $pid = fork){
    while (waitpid(-1,&WNOHANG) != $pid){
    print ".";
    sleep 2;
    }

    print qq{
    </body>
    </html>
    };

    exit;
    }
    else {
    die "Cannot fork: $!\n" unless defined $pid;

    #### Call the actual processing script.

    print "Executing $program $args\n";
    ! system("perl", "$program", $args) or die "Call failed: $!";
    #print $output;

    exit;
    }

    #### End Calling script
    #### Called script

    #!/usr/local/bin/perl

    use CGI;
    use strict;

    my $query = new CGI;
    my $sleep_time = $query->param("sleep");
    my @params = $query->param;

    print "Content-type: text/html\n\n";
    print qq{
    <html>
    <head><title>INF</title>
    </head>
    <body>
    @params
    };

    for (my $i = 0; $i < 10; $i++){
    print "Working\n";
    sleep $sleep_time;
    }


    print qq{
    Sleep time was $sleep_time<br>
    Finished
    </body>
    </html>
    };

    exit;
    #### End Called script
    Shailan, Dec 12, 2003
    #1
    1. Advertising

  2. Shailan

    Alex Zeng Guest

    The problem is the called CGI program is no longer a "CGI". It is just a
    regular program. You should look for the parameters from the command line,
    instead of using $query->param, which fetches the param from env var
    QUERY_STRING, of course it is empty in this case.

    So try to get the param from command line, like regular program in you
    called "CGI" program like this,

    $ARGV[0] $ARGV[1] ....

    You will be doing fine

    "Shailan" <> wrote in message
    news:...
    > Hi
    >
    > Im having trouble with the following code that seems to be behave
    > differently when called from the browser as opposed to the command
    > line. The calling script is a cgi that forks, with the child trying to
    > call another cgi script and pass arguments to it. It works fine from
    > the command line, and calls the required script and passes the
    > arguments correctly. However, when it is run on the browser, it calls
    > the script but does not pass the arguments to it.
    > The called script (included) is also a cgi, set up to accept
    > parameters via the param function. It accepts the parameters when the
    > calling script is run on the command line, but not when the calling
    > script is run on the browser.
    >
    > I have also tried putting a '?' between the script name and the
    > arguments to get the "script.cgi?argument=1" format, and that didnt
    > work either.
    >
    > Im not sure if the problem is in the system call, the use of CGI in
    > the called script, or somewhere else entirely. Any suggestions would
    > be extremely helpful...
    >
    > Thanks in advance
    > Shailan
    >
    > #### Calling script
    >
    > if (my $pid = fork){
    > while (waitpid(-1,&WNOHANG) != $pid){
    > print ".";
    > sleep 2;
    > }
    >
    > print qq{
    > </body>
    > </html>
    > };
    >
    > exit;
    > }
    > else {
    > die "Cannot fork: $!\n" unless defined $pid;
    >
    > #### Call the actual processing script.
    >
    > print "Executing $program $args\n";
    > ! system("perl", "$program", $args) or die "Call failed: $!";
    > #print $output;
    >
    > exit;
    > }
    >
    > #### End Calling script
    > #### Called script
    >
    > #!/usr/local/bin/perl
    >
    > use CGI;
    > use strict;
    >
    > my $query = new CGI;
    > my $sleep_time = $query->param("sleep");
    > my @params = $query->param;
    >
    > print "Content-type: text/html\n\n";
    > print qq{
    > <html>
    > <head><title>INF</title>
    > </head>
    > <body>
    > @params
    > };
    >
    > for (my $i = 0; $i < 10; $i++){
    > print "Working\n";
    > sleep $sleep_time;
    > }
    >
    >
    > print qq{
    > Sleep time was $sleep_time<br>
    > Finished
    > </body>
    > </html>
    > };
    >
    > exit;
    > #### End Called script
    Alex Zeng, Dec 13, 2003
    #2
    1. Advertising

  3. Shailan

    Shailan Guest

    Thanks Alex...that clears it up.

    "Alex Zeng" <> wrote in message news:<>...
    > The problem is the called CGI program is no longer a "CGI". It is just a
    > regular program. You should look for the parameters from the command line,
    > instead of using $query->param, which fetches the param from env var
    > QUERY_STRING, of course it is empty in this case.
    >
    > So try to get the param from command line, like regular program in you
    > called "CGI" program like this,
    >
    > $ARGV[0] $ARGV[1] ....
    >
    > You will be doing fine
    >
    > "Shailan" <> wrote in message
    > news:...
    > > Hi
    > >
    > > Im having trouble with the following code that seems to be behave
    > > differently when called from the browser as opposed to the command
    > > line. The calling script is a cgi that forks, with the child trying to
    > > call another cgi script and pass arguments to it. It works fine from
    > > the command line, and calls the required script and passes the
    > > arguments correctly. However, when it is run on the browser, it calls
    > > the script but does not pass the arguments to it.
    > > The called script (included) is also a cgi, set up to accept
    > > parameters via the param function. It accepts the parameters when the
    > > calling script is run on the command line, but not when the calling
    > > script is run on the browser.
    > >
    > > I have also tried putting a '?' between the script name and the
    > > arguments to get the "script.cgi?argument=1" format, and that didnt
    > > work either.
    > >
    > > Im not sure if the problem is in the system call, the use of CGI in
    > > the called script, or somewhere else entirely. Any suggestions would
    > > be extremely helpful...
    > >
    > > Thanks in advance
    > > Shailan
    > >
    > > #### Calling script
    > >
    > > if (my $pid = fork){
    > > while (waitpid(-1,&WNOHANG) != $pid){
    > > print ".";
    > > sleep 2;
    > > }
    > >
    > > print qq{
    > > </body>
    > > </html>
    > > };
    > >
    > > exit;
    > > }
    > > else {
    > > die "Cannot fork: $!\n" unless defined $pid;
    > >
    > > #### Call the actual processing script.
    > >
    > > print "Executing $program $args\n";
    > > ! system("perl", "$program", $args) or die "Call failed: $!";
    > > #print $output;
    > >
    > > exit;
    > > }
    > >
    > > #### End Calling script
    > > #### Called script
    > >
    > > #!/usr/local/bin/perl
    > >
    > > use CGI;
    > > use strict;
    > >
    > > my $query = new CGI;
    > > my $sleep_time = $query->param("sleep");
    > > my @params = $query->param;
    > >
    > > print "Content-type: text/html\n\n";
    > > print qq{
    > > <html>
    > > <head><title>INF</title>
    > > </head>
    > > <body>
    > > @params
    > > };
    > >
    > > for (my $i = 0; $i < 10; $i++){
    > > print "Working\n";
    > > sleep $sleep_time;
    > > }
    > >
    > >
    > > print qq{
    > > Sleep time was $sleep_time<br>
    > > Finished
    > > </body>
    > > </html>
    > > };
    > >
    > > exit;
    > > #### End Called script
    Shailan, Dec 15, 2003
    #3
    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. Achim Domma (Procoders)

    read input for cmd.Cmd from file

    Achim Domma (Procoders), Jun 3, 2005, in forum: Python
    Replies:
    2
    Views:
    8,027
    Peter Otten
    Jun 3, 2005
  2. Sarir Khamsi

    Interpreter-like help in cmd.Cmd

    Sarir Khamsi, Jun 9, 2005, in forum: Python
    Replies:
    4
    Views:
    346
    Bengt Richter
    Jun 26, 2005
  3. THTB
    Replies:
    0
    Views:
    167
  4. Rob
    Replies:
    1
    Views:
    151
    Jeff Cochran
    Oct 18, 2004
  5. Max Williams
    Replies:
    3
    Views:
    149
    Robert Klemme
    Jan 6, 2009
Loading...

Share This Page