Using Socket to call a Perl CGI : Socket « Network « Perl






Using Socket to call a Perl CGI

    

use IO::Socket;

$name = 'Tom';
$email = 'a@a.com';

$string = '?' . "name=" . $name . "&" . "email=" . $email;
$string =~ tr/ /+/;

$socket = IO::Socket::INET->new
( 
    Proto     => "tcp",
    PeerAddr  => "yourserver.com",
    PeerPort  => 80,
);

$socket->autoflush(1);

print $socket "GET /reg.cgi$string ', 'HTTP/1.0\015\012\015\012";

while ($line = <$socket>) {
    $results .= $line
}

close $socket;
print $results;
if ($results =~ /Thanks for registering./mg) {

    print "Success."

} else {

    print "Sorry, there was an error."

}

#File: reg.cgi


#!/usr/local/bin/perl

use CGI;                             

$co = new CGI;                        

print $co->header,                    

$co->start_html(
    -title=>'CGI Example', 
    -author=>'yourName', 
    -meta=>{'keywords'=>'CGI Perl'}, 
    -BGCOLOR=>'white', 
    -LINK=>'red'
);

if ($co->param()) {
    $! = 0;
    open FILEHANDLE, ">>reg.log";
    print FILEHANDLE "Date: " . `date`;
    print FILEHANDLE "Name: " . $co->param('name') . "\n";
    print FILEHANDLE "email: " . $co->param('email') . "\n";
    close FILEHANDLE;
    unless ($!) {
        print "Success.";
    } else {
        print "Sorry, there was an error: $!";
    }
}
print $co->end_html;                  

   
    
    
    
  








Related examples in the same category

1.Time Server with Socket
2.Socket server
3.Perl Modules for Networking with Sockets
4.Open a socket
5.Post query to a CGI
6.Query a Perl CGI
7.Listen to a port
8.Using regular expresion to validate an IP address
9.Child handle and parent handle
10.Daytime client, using symbolic host and service names
11.Add a host, delete a host, add a user, delete a user, ping a host, list processes, list filesystems, lists hosts, and kill a process.
12.A Perl TCP server without the Socket module.
13.A Simple script to update your host/ip with dyndns.org service.