Specialized Networking APIs for Perl

 < Day Day Up > 



Perl includes a number of specialized APIs for a number of Application layer protocols (some are included, others must be installed via CPAN). These include SMTP, HTTP, CGI, POP3, FTP, Telnet, and others. In this section, we look examples of the SMTP and HTTP client-side modules.

Perl Net::SMTP Module

SMTP is the Simple Mail Transfer Protocol and is used to transfer e-mail to a mail server that delivers it to the final destination. Perl's Net::SMTP module provides a very flexible SMTP interface, as is illustrated in Listing 12.2.

Listing 12.2 Sample Net::SMTP client example (smtpc.pl).

start example
use Net::SMTP; my $server = "mail.mtjones.com"; $smtp = Net::SMTP->new( $server ) or   die "Couldn't connect to the server"; $smtp->mail( "tim\@mtjones.com" ); $smtp->to( "mtj\@mtjones.com" ); $smtp->data(); $smtp->datasend("Subject: Test email\n"); $smtp->datasend("Content-Type: text/html\n"); $smtp->datasend("\n"); $smtp->datasend(    "<HTML><BODY><H1>This is the test email</H1></BODY></HTML>" ); $smtp->dataend(); $smtp->quit();
end example

The very simple source in Listing 12.2 illustrates sending a short HTML encoded e-mail. We make the Net::SMTP module visible using use and then create a local string variable to hold our server address ($server). Using the new constructor of Net::SMTP, we create a new SMTP object named $smtp. We pass in the server name to which we'll ultimately connect to send our mail (the outgoing mail transfer agent).

We then begin using the $smtp object to load the e-mail to send. Using the mail function, we specify from whom the e-mail is coming and using to, we specify to whom the e-mail will go. The data() function begins the e-mail transaction, which is then followed by a number of datasend functions. The datasend function is simply a way of providing the $smtp object with a set of data to send to the server. In this case, we send the subject of the e-mail and the content type (to allow the client to understand how to render the final e-mail). We end the datasend with a blank line, to separate the optional SMTP headers from the body of the e-mail. The next datasend contains the body of the e-mail followed by a dataend that specifies that the e-mail transaction is complete (terminates the SMTP session with the server). Finally, we call the quit method to close the session with the server.

Perl LWP::Simple Module

HTTP is the classic Web transport protocol used to transfer Web pages (and other content) across the Internet. Numerous methods exist for Perl in HTTP, but the simplest is likely the LWP package (libwww-perl). A sample usage of LWP::Simple for HTTP client functionality is shown in Listing 12.3.

Listing 12.3 Sample LWP::Simple client example (httpc.pl).

start example
use LWP::Simple; $url = "http:://www.microsoft.com"; getprint( $url ) or die "getprint failed.";
end example

In the example shown here (Listing 12.3), the LWP::Simple module is used to grab and print content from a given URL ($url). The getprint function retrieves and then prints the HTML page. LWP provides a number of modules for accessing Web content. The simplest is the Simple module; for more complex operations, the LWP::UserAgent can be used.

That's extremely simple, compared to what actually goes on underneath the covers of HTTP. Therefore, Perl and its accompanying modules provide some very useful and simplifying abstractions for higher-level protocols.



 < Day Day Up > 



BSD Sockets Programming from a Multi-Language Perspective
Network Programming for Microsoft Windows , Second Edition (Microsoft Programming Series)
ISBN: 1584502681
EAN: 2147483647
Year: 2003
Pages: 225
Authors: Jim Ohlund

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net