📄 ch17_02.htm
字号:
<html><head><title>Net::NNTP (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 & 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="ch17_01.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="ch17_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">17.2. Net::NNTP</h2><a name="INDEX-2208" /><a name="INDEX-2209" /><a name="INDEX-2210" /><p><a name="INDEX-2211" /><a name="INDEX-2212" /><a name="INDEX-2213" />Net::NNTP is one of the<em class="emphasis">libnet</em> modules. It provides methods forprogramming a news client to interface with an NNTP server. Net::NNTPimplements the NNTP protocol as defined in RFC 977, as well as anumber of extensions defined in the IETF Internet Draft documentmentioned earlier.</p><p>For example, the following code: </p><blockquote><pre class="code">$nntp->post(@message);</pre></blockquote><p>is equivalent to issuing the <em class="emphasis">post</em> command andassociated X-headers directly to an NNTP server on port 119.</p><a name="perlnut2-CHP-17-SECT-2.1" /><div class="sect2"><h3 class="sect2">17.2.1. Initializing the NNTP Client</h3><p><a name="INDEX-2214" />Touse Net::NNTP, create a new Net::NNTP object:</p><blockquote><pre class="code">use Net::NNTP; $nntp = Net::NNTP->new; # Use default port and options</pre></blockquote><p>Once you've created the object, you can use any ofthe Net::NNTP methods on that object. The Net::NNTP methods generallyparallel the functionality of the raw NNTP commands. Your news clientcan be written to perform many functions, including:</p><ul><li><p>Authenticating a user to the server</p></li><li><p>Listing available newsgroups</p></li><li><p>Retrieving news headers and articles</p></li><li><p>Extracting headers</p></li><li><p>Checking for new articles</p></li><li><p>Posting articles</p></li><li><p>Listing information such as active messages in a newsgroup, newnewsgroups, valid distribution areas, or message IDs of newly arrivedmessages</p></li></ul></div><a name="perlnut2-CHP-17-SECT-2.2" /><div class="sect2"><h3 class="sect2">17.2.2. Net::NNTP Reference</h3><p>Net::NNTP includes methods that implement many of the extensions toRFC 977; the description indicates if a method is an extension.Extensions that are not supported by Net::NNTP are AUTHINFO, GENERIC,XINDEX, XSEARCH, and XTHREAD. In addition, some extensions supportedby Net::NNTP may not be supported by a particular server.</p><p>Unless otherwise stated, all the methods return either true or falseto indicate success or failure. If a method returns a value instead,then it returns <tt class="literal">undef</tt> or an empty list to indicatefailure.</p><p>In this list, <em class="emphasis">message-spec</em> refers to a singlemessage ID, a single message number, or a reference to a list of twomessage numbers. It can also be passed as a list of two messagenumbers, but this is for compatibility only and is now deprecated.</p><p>Where pattern-matching is indicated, the matching is done accordingto the NNTP specification. See the Net::NNTP documentation fordetails.</p><a name="INDEX-2215" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">nntp</em> = Net::NNTP->new([<em class="replaceable">host</em>[, <em class="replaceable">options</em>]])</pre><p><a name="INDEX-2215" />Constructor. Creates a new Net::NNTPobject. Arguments are:</p><dl><dt><i><em class="replaceable"><tt>host</tt></em></i></dt><dd>The name of the remote host to which an NNTP connection is required.If not specified, the constructor checks the environment variablesNNTPSERVER and NEWSHOST, in that order, then Net::Config. Defaults to<tt class="literal">news</tt> if nothing is found.</p></dd><dt><i><em class="replaceable"><tt>options</tt></em></i></dt><dd>Options. Passed as key/value pairs. Possible options are:</p><dl><dt><b><tt class="literal">Timeout</tt></b></dt><dd>Maximum time, in seconds, to wait for a response from the NNTPserver. Default is 120 seconds. A value of <tt class="literal">0</tt>causes all I/O operations to block.</p></dd><dt><b><tt class="literal">Debug</tt></b></dt><dd>Enables printing of debugging information to STDERR.</p></dd></dl></dd></dl></div><a name="INDEX-2216" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>active</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">nntp</em>->active([<em class="replaceable">pattern</em>])</pre><p><a name="INDEX-2216" />Similar to<tt class="literal">list</tt>, but returns only active groups that matchthe pattern (an extension).</p></div><a name="INDEX-2217" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>active_times</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">nntp</em>->active_times( )</pre><p><a name="INDEX-2217" />Returns a reference to a hash inwhich the keys are the group names, and the values are references toarrays containing the time the group was created and an identifier,possibly an email address, for the creator (an extension).</p></div><a name="INDEX-2218" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>article</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">nntp</em>->article([<em class="replaceable">msgid</em>|<em class="replaceable">msgnum</em>])</pre><p><a name="INDEX-2218" />Retrieves the header, a blankline, then the body (text) of the article, specified as either amessage ID or a message number. With no arguments, returns thecurrent article in the current newsgroup as a reference to an arraycontaining the article.</p></div><a name="INDEX-2219" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>authinfo</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">nntp</em>->authinfo(<em class="replaceable">user</em>, <em class="replaceable">pass</em>)</pre><p><a name="INDEX-2219" />Authenticates the user to theserver, passing the user's username and password.</p></div><a name="INDEX-2220" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>body</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">nntp</em>->body([<em class="replaceable">msgid</em>|<em class="replaceable">msgnum</em>])</pre><p><a name="INDEX-2220" />Retrieves the body (text) of thearticle specified by either a message ID or a message number. Takesthe same arguments as <tt class="literal">article</tt>. Returns a referenceto an array containing the body of the article.</p></div><a name="INDEX-2221" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>date</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">nntp</em>->date( )</pre><p><a name="INDEX-2221" />Returns the date on the remoteserver, in Unix time format (seconds since 1970).</p></div><a name="INDEX-2222" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>distributions</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">nntp</em>->distributions( )</pre><p><a name="INDEX-2222" />Returns a reference to a hash inwhich the keys are the valid distribution names, and the values arethe distribution descriptions (an extension).</p></div><a name="INDEX-2223" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>group</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">nntp</em>->group([<em class="replaceable">group</em>])</pre><p><a name="INDEX-2223" />Sets and/or gets the current group.With no argument, returns information about the current group;otherwise, sets the current group to<em class="replaceable"><tt>group</tt></em>.</p><p>In a scalar context, <tt class="literal">group</tt> returns the group name.In an array context, it returns a list containing the number ofarticles in the group, the first article number, the last articlenumber, and the group name.</p></div><a name="INDEX-2224" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>head</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">nntp</em>->head([<em class="replaceable">msgid</em>|<em class="replaceable">msgnum</em>])</pre><p><a name="INDEX-2224" />Retrieves the header of the articlespecified by <em class="replaceable"><tt>msgid</tt></em> or<em class="replaceable"><tt>msgnum</tt></em>. Takes the same arguments as<tt class="literal">article</tt> and returns a reference to an arraycontaining the article's header.</p></div><a name="INDEX-2225" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>ihave</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">nntp</em>->ihave(<em class="replaceable">msgid</em>[, <em class="replaceable">message</em>])</pre><p><a name="INDEX-2225" />Informs the server that the clienthas an article whose ID is <em class="replaceable"><tt>msgid</tt></em>. Theoptional argument <em class="replaceable"><tt>message</tt></em> can be either anarray of lines or a reference to an array. If<em class="replaceable"><tt>message</tt></em> is specified, and the server wantsa copy, it is sent, returning true on successful completion.</p><p>If <em class="replaceable"><tt>message</tt></em> is not specified, then themessage must be sent using the Net::Cmd <tt class="literal">datasend</tt>and <tt class="literal">dataend</tt> methods.</p></div><a name="INDEX-2226" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>last</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">nntp</em>->last( )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -