⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch20_01.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>The LWP Library (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="part8.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch20_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h1 class="chapter">Chapter 20. The LWP Library</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4>  <p> <a href="#perlnut2-CHP-20-SECT-1">LWP Overview</a><br /><a href="ch20_02.htm">The LWP Modules</a><br /><a href="ch20_03.htm">The HTTP Modules</a><br /><a href="ch20_04.htm">The HTML Modules</a><br /><a href="ch20_05.htm">The URI Module</a><br /></p></div><p><a name="INDEX-2373" /></a>The library forweb access in Perl (LWP), is a bundle of modules that provide aconsistent, object-oriented approach to creating web applications.The library, downloaded as the single file named<em class="emphasis">libwww-perl</em>, contains the followingclasses<a name="INDEX-2374" /></a><a name="INDEX-2375" /></a>:</p><dl><dt><i>File</i></dt><dd>Parses directory listings.<p></p></dd><dt><i>Font</i></dt><dd>Handles Adobe Font Metrics.<p></p></dd><dt><i>HTML</i></dt><dd>Parses HTML files and converts them to printable or other forms.<p></p></dd><dt><i>HTTP</i></dt><dd>Provides client requests, server responses, and protocolimplementation.<p></p></dd><dt><i>LWP</i></dt><dd>The core of all web client programs. It creates network connectionsand manages the communication and transactions between client andserver.<p></p></dd><dt><i>URI</i></dt><dd>Creates, parses, and translates URLs.<p></p></dd><dt><i>WWW</i></dt><dd>Implements standards used for robots (automatic client programs).<p></p></dd></dl><p>Each module in LWP provides different building blocks that allow youto do basically anything you can do in a Web browser&#x2014;fromconnection, to request, to response and returned data. These partsare then encapsulated by an object to give a standard interface toevery web program you write.</p><p>This chapter covers all the modules in the LWP distribution. Whilesome modules are more useful than others, everything you need to knowabout LWP is included here. To begin with, the following sectiongives an overview of how LWP works to create a web client.</p><div class="sect1"><a name="perlnut2-CHP-20-SECT-1" /></a><h2 class="sect1">20.1. LWP Overview</h2><p>Any web transaction requires an application that can establish aTCP/IP network connection and send and receive messages using theappropriate protocol (usually HTTP). TCP/IP connections areestablished using sockets, and messages are exchanged via socketfilehandles. (See <a href="ch15_01.htm">Chapter 15, "Sockets"</a> for information on howto manually create socket applications.) LWP provides an object forthis application with LWP::<a name="INDEX-2376" /></a><a name="INDEX-2377" /></a><a name="INDEX-2378" /></a>UserAgent for clients; HTTP::Daemonprovides a server object. The UserAgent object acts as the browser:it connects to a server, sends requests, receives responses, andmanages the received data. This is how you create a UserAgent object:</p><blockquote><pre class="code">use LWP::UserAgent;$ua = LWP::UserAgent-&gt;new( );</pre></blockquote><p>The UserAgent now needs to send a message to a server requesting aUniversal Resource Locator (URL) using the <tt class="literal">request</tt>method. <tt class="literal">request</tt> forms an HTTP request from theobject given as its argument. This request object is created byHTTP::Request<a name="INDEX-2379" /></a><a name="INDEX-2380" /></a><a name="INDEX-2381" /></a>.</p><p><a name="INDEX-2382" /></a><a name="INDEX-2383" /></a>An HTTP request messagecontains three elements. The first line of a message always containsan HTTP command called a <em class="emphasis">method</em>; a UniversalResource Identifier (URI), which identifies the file or resource theclient is querying; and the HTTP version number. The following linesof a client request contain header information, which providesinformation about the client and any data it is sending the server.The third part of a client request is the entity body, which is datasent to the server (for the POST method). The following is a sampleHTTP request:</p><blockquote><pre class="code">GET /index.html HTTP/1.0User-Agent: Mozilla/1.1N (Macintosh; I; 68K)Accept: */*Accept: image/gifAccept: image/jpeg</pre></blockquote><p><tt class="literal">LWP::UserAgent-&gt;request</tt> forms this message froman HTTP::Request object. A request object requires a method for thefirst argument. The GET method asks for a file, while the POST methodsupplies information such as form data to a server application. Thereare other methods, but these two are most commonly used.</p><p>The second argument is the URL for the request. The URL must containthe server name because this is how the UserAgent knows where toconnect. The URL <a name="INDEX-2384" /></a><a name="INDEX-2385" /></a>argument can be represented as a stringor as a URI::URL object, which allows more complex URLs to be formedand managed. Optional parameters for an HTTP::Request include yourown headers, in the form of an HTTP::Headers object, and any POSTdata for the message. The following example creates a request object:</p><blockquote><pre class="code">use HTTP::Request;$req = HTTP::Request-&gt;new(GET, $url, $hdrs);</pre></blockquote><p>The URL object is created like this: </p><blockquote><pre class="code">use URI::URL;$url = URI::URL-&gt;new('www.ora.com/index.html');</pre></blockquote><p>And a header object can be created like this: </p><blockquote><pre class="code">use HTTP::Headers;$hdrs = HTTP::Headers-&gt;new(Accept =&gt; 'text/plain',                          User-Agent =&gt; 'MegaBrowser/1.0');</pre></blockquote><p>Then you can put them all together to make a request: </p><blockquote><pre class="code">use LWP::UserAgent;  # This will cover all of them!$hdrs = HTTP::Headers-&gt;new(Accept =&gt; 'text/plain',                          User-Agent =&gt; 'MegaBrowser/1.0');$url = URI::URL-&gt;new('www.ora.com/index.html');$req = HTTP::Request-&gt;new(GET, $url, $hdrs);$ua = LWP::UserAgent-&gt;new( );$resp = $ua-&gt;request($req);if ($resp-&gt;is_success) {        print $resp-&gt;content;}else {        print $resp-&gt;message;}</pre></blockquote><p><a name="INDEX-2386" /></a><a name="INDEX-2387" /></a>Oncethe request has been made by the user agent, the response from theserver is returned as another object, described by HTTP::Response.This object contains the status code of the request, returnedheaders, and the content you requested, if successful. In theexample, <tt class="literal">is_success</tt> checks to see if the requestwas fulfilled without problems, thus outputting the content. Ifunsuccessful, a message describing the server'sresponse code is printed.</p><p>There are other modules and classes that create useful objects forweb clients in LWP, but the above examples show the most basic ones.For server applications, many of the objects used above become piecesof a server transaction, which you either create yourself (such asresponse objects) or receive from a client (such as request objects).</p><p>Additional functionality for both client and server applications isprovided by the HTML module. This module provides many classes forboth the creation and interpretation of HTML documents.</p><p>The rest of this chapter provides information for the LWP, HTTP,HTML, and URI modules.</p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="part8.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch20_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">VIII. LWP</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">20.2. The LWP Modules</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm">      </map></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -