next up previous
Next: 0.6 Graph Packages Up: CGI-Perl, GnuPlot, etc - Previous: 0.4.1 Using GNUPlot inside

0.5 LWP

 
[L529guest@biokdd agopu]\$ cat > lwp.pl
 #!/usr/bin/perl
                                                                                                                             
 # Create a user agent object
 use LWP::UserAgent;
                                                                                                                             
 use CGI;
 $query = new CGI;
                                                                                                                             
 print $query->header;
                                                                                                                            
 $ua = LWP::UserAgent->new;
 $ua->agent("MyApp/0.1 ");
                                                                                                                             
 # Create a request
 my $req = HTTP::Request->new(POST => 'http://www.perl.com/');
 $req->content_type('application/x-www-form-urlencoded');
# $req->content('match=www&errors=0');
                                                                                                                             
 # Pass request to the user agent and get a response back
 my $res = $ua->request($req);
                                                                                                                             
 # Check the outcome of the response
 if ($res->is_success) {
     print $res->content;
 } else {
     print "Bad luck this time\n";
 }
                                                                                                                            
 print $query->end_html;
 
[L529guest@biokdd agopu]$ chmod 755 *.pl

If things go according to plan, you should be redirected to www.perl.com on your browser if you visit:

http://biokdd.informatics.indiana.edu/cgi-bin/L529guest/agopu/lwp.pl

(Replace `agopu' with your user name or whatever you named your directory to be. Remove `L529guest' if you are using your own account.)

Ofcourse what has been abstracted out of this dicussion is that if the server (www.per.com in this case) refused a connection (say, because we had connected less than `n' secs back) then the script would have printed ``Bad luck this time'' instead of taking you to per.com. That is the whole objective (in this example, that is; LWP has a lot more capabilities) of using LWP - to prevent hoggin a server with a deluge of requests. We can setup the script to send requests, for instance, once every `n' seconds. That is left as an exercise!

Hint: Try using ``sleep n-seconds' in your CGI script

OR

See here: http://bio.informatics.indiana.edu/L529/lab/lwp.pl.sample


next up previous
Next: 0.6 Graph Packages Up: CGI-Perl, GnuPlot, etc - Previous: 0.4.1 Using GNUPlot inside
Arvind Gopu 2006-03-24