I have a perl program (version of perl being 5.005_03) which needs to execute a cgi script residing in a remote machine. This CGI script will change the text in a file based on the parameters passed while calling the script.
For eg: Perl is in servername1 machine and it has to execute the CGI script on servername2, something like "www.servername2.com/cg-bin/test.pl?good".
Since the version of perl does not support the LWP, I am forced to use an alternative. I cannot upgrade the perl version due to some limitations.
I am confused as to how this can be achieved. Can I use GET/POST method, or any other perl modules like HTTP::New...I am really unaware on this.
Please Help!
Thanks very much in advance....
If you do have Perl on both servers, and can execute the remote script in a browser, but you just can't use LWP, can you do "use Socket;" and just make a raw HTTP socket connection to the other server? Something like:
use Socket;
my $web_server_hostname = "www.servername2.com";
my $this = pack_sockaddr_in(0, inet_aton('localhost'));
my $that = pack_sockaddr_in(80,inet_aton($web_server_hostname));
if (socket(S, AF_INET, SOCK_STREAM, $proto)) {
print "Created socket.";
}
else { print "Could not create socket for www: $!"; }
if (connect(S,$that)) {
print "connected to socket";
}
else { print "Could not connect to socket: $!"; }
select(S); $¦ = 1; select(STDOUT); $¦ = 1;
print S "GET /cgi-bin/otherscript.cgi?Arg1=something&arg2=whatever HTTP/1.0\n";
print S "HOST: $web_server_hostname\n\n";
while(<S>[smilestopper]){
print STDOUT $_;
}
close(S);
(not sure if Socket.pm came with that version of Perl or not. I'm also not sure this will be cross-platform compatible, but I've tried it on Linux (Fedora Core 1 and 2) and Windows NT 4/2000/XP.)
"/cgi-bin/otherscript.cgi?Arg1=something&arg2=whatever" is the URL of the script you're trying to call with all the arguments in GET format, but without the hostname.
Oh, and be careful if you copy and paste this, because WW changes the ¦ symbol into something else. You'll need to change those "$¦ = 1" to use the real ¦ symbol...
JK
I used the GET command to access the remote CGI script and checked the return values. I am able to access the remote script in this way.
Thanks again...
$external_url= 'http://example.com/cgi-bin/external_script.pl';
# For get
$result = `curl $external_url`;
## For post
$result = `curl -d 'param1-one¶m2=2&and=so&forth=1' $external_url`;
-d tells curl to manage the parameter immediately following it as data to be posted.
In either case, $result will contain the entire response from $external_url - usually a whole web page unless $external_url is set to print just response data. So if $external_url is just
#!/usr/bin/perl
print "Hello";
$result will just contain "Hello".;
I'd be concerned that the & signs would be interpreted by the shell, but putting it in single quotes should hopefully prevent that.