in reply to Simulating Form Posts from within PERL

First, it's Perl - not PERL. :) Second, LWP is GOOOOOD!

Moving on, consider the following CGI script, which i named simple.cgi:

#!/usr/bin/perl -Tw use strict; use CGI qw(:standard); print header; if (param('foo')) { print "you sent ", param('foo'); } else { print start_html,start_form,textfield('foo'), submit,end_form,end_html, ; }
The following LWP script will talk to it:
use strict; use LWP; use HTTP::Request::Common; my $url = 'http://localhost/cgi-bin/simple.cgi'; my $ua = LWP::UserAgent->new; my $request = POST($url, Content => [foo=>'hello world']); my $response = $ua->request($request); print $response->content, "\n";
Now, was that so bad? CGI.pm and LWP.pm are your friends. :) Use them! Just to show that i am a nice guy, here is an example that uses IO::Socket and GET to do roughly the same thing (oh, you were forgetting to read the results back, by the way):
use strict; use IO::Socket qw(:DEFAULT :crlf); $/ = CRLF . CRLF; my $sock = IO::Socket::INET->new( Proto => 'tcp', PeerAddr => 'localhost', PeerPort => 'http(80)', ); print $sock "GET /cgi-bin/simple.cgi?foo=hello%20world",$/; my $header = <$sock>; print $header; my $data; print $data while read($sock,$data,1024) > 0;
Much uglier in my opinion, and definitely less robust. If you plan to stick with raw sockets, then i suggest you buy and read Network Programming with Perl. Good luck! :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: (jeffa) Re: Simulating Form Posts from within PERL
by flappy (Novice) on Aug 02, 2002 at 03:56 UTC
    ok Jeffa, thank you very much for your detailed suggestions, I'll try it out.