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

📄 ch17_01.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>Usenet News (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="ch16_02.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_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h1 class="chapter">Chapter 17. Usenet News</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4>  <p> <a href="#perlnut2-CHP-17-SECT-1">The NNTP Protocol</a><br /><a href="ch17_02.htm">Net::NNTP</a><br /><a href="ch17_03.htm">The News::Newsrc Module</a><br /></p></div><p>Usenet is a collection of bulletin board-like newsgroups on theInternet, covering thousands of topics. Whatever your interest,chances are you'll find a newsgroup in which it isdiscussed<a name="INDEX-2189" /></a><a name="INDEX-2190" /></a><a name="INDEX-2191" /></a>. </p><p>Usenet has been around since late 1979. The current implementation isbased on the Network <a name="INDEX-2192" /></a><a name="INDEX-2193" /></a>NewsTransfer Protocol (NNTP), defined inRFC 977 and released in March 1986. Information is propagated throughUsenet by a system of newsfeeds in which one site requests a newsfeedfrom another site, and a third site requests a newsfeed from thesecond site, etc. There is no central Usenet authority&#x2014;likePerl, it runs on the spirit of cooperation and sharing. When you runa news reader, such as <em class="emphasis">tin</em> or the news reader ina web browser, your client software talks to the NNTP server on thenews host. When you post a message to a newsgroup, this posting isreceived by your NNTP server and passed on to other serversthroughout the distribution area you specified for the posting. Eachserver periodically receives updated newsgroup information and newlyposted news articles.</p><p>This chapter explores NNTP commands and responses. It introducesNet::NNTP, which implements NNTP commands and simplifies the processof writing a Perl-based NNTP news client. It also describesNews::Newsrc, a module that provides methods for managing a<em class="emphasis">.newsrc</em> file.</p><p>There are two kinds of NNTP commands: the official set of commands asdefined in RFC 977 and a number of extensions that have been addedsince the RFC was written. The extensions are described in an<a name="INDEX-2194" /></a>IETF InternetDraft document, "Common NNTPExtensions" by Stan Barber, which can be found atftp://ftp.academ.com/pub/nntp/ietf/nntpext.txt.</p><div class="sect1"><a name="perlnut2-CHP-17-SECT-1" /></a><h2 class="sect1">17.1. The NNTP Protocol</h2><p><a name="INDEX-2195" /></a>Before you write your own newsclient, you should have some idea of how the NNTP protocol works.Like other servers, an NNTP server is bound to a port (usually port119). It listens for incoming connections, takes the appropriateaction, and returns a response to the client. When a news clientconnects with an NNTP server, or to the port on which the NNTP serveris running, a message such as the following is produced:</p><blockquote><pre class="code">Trying 0.0.0.0...Connected to hostname.mydomain.com.Escape character is '^]'.200 newshost.mydomain.com InterNetNews NNRP server INN 1.5.1 17-Dec-1996 ready(posting ok).</pre></blockquote><p>Many NNTP servers understand the <em class="emphasis">help</em> (or<em class="emphasis">HELP</em>) command. When a client issues a<em class="emphasis">help</em> command, many NNTP servers respond with alist of all available commands. For example:</p><blockquote><pre class="code">200 news.mydomain.com InterNetNews NNRP server INN 1.5.1 17-Dec-1996 ready(posting ok).HELP100 Legal commands  authinfo user Name|pass Password|generic &lt;prog&gt; &lt;args&gt;  article [MessageID|Number]  body [MessageID|Number]  date  group newsgroup  head [MessageID|Number]  help  ihave  last  list [active|active.times|newsgroups|distributions|distrib.pats|overview.fmt|subscriptions]  listgroup newsgroup  mode reader  newgroups yymmdd hhmmss ["GMT"] [&lt;distributions&gt;]  newnews newsgroups yymmdd hhmmss ["GMT"] [&lt;distributions&gt;]  next  post  slave  stat [MessageID|Number]  xgtitle [<em class="replaceable"><tt>group_pattern</tt></em>]  xhdr header [range|MessageID]  xover [<em class="replaceable"><tt>range</tt></em>]  xpat header range|MessageID pat [morepat...]  xpath MessageIDReport problems to &lt;usenet@news.mydomain.com&gt;.</pre></blockquote><p>After connecting to the NNTP server, you can get a list of<a name="INDEX-2196" /></a><a name="INDEX-2197" /></a>available newsgroups with the<em class="emphasis">list active</em> command, and you can then select anewsgroup. If you were to connect directly to<em class="emphasis">news.mydomain.com</em> and use the<em class="emphasis">group</em> command to select the newsgroup<em class="emphasis">local.test</em>, your session might look like this:</p><blockquote><pre class="code">200 news.mydomain.com InterNetNews NNRP server INN 1.5.1 17-Dec-1996 ready(posting ok).group local.test211 4 1 4 local.testQUIT205 .</pre></blockquote><p><a name="INDEX-2198" /></a>The fournumbers (in this example, <tt class="literal">211 4 1 4</tt>) preceding thegroup name (<em class="emphasis">local.test</em>) represent the successcode, total number of articles, and the first and last articlenumbers, respectively. So in this example, <tt class="literal">211</tt> isthe success code. There are four articles in the<em class="emphasis">local.test</em> group, starting with article number 1and ending with number 4.</p><p><a name="INDEX-2199" /></a>Every articlein a newsgroup has two identifiers associated with it. The first isthe message number, and the second is a message ID. The messagenumber is the number of that article on the server to which you areconnecting. The message ID is an identifier that is always associatedwith the article on every server that receives the article. Anarticle that has the message number 4 on your server might be number83 on someone else's server. The message ID,however, will be the same on both. So, for example, if you want topost an article that refers to another article, use the message ID.</p><p><a name="INDEX-2200" /></a><a name="INDEX-2201" /></a><a name="INDEX-2202" /></a>Once the <em class="emphasis">group</em>command has successfully identified a newsgroup, the client canrequest a particular article by sending an<em class="emphasis">article</em> request to the server. The<em class="emphasis">article</em> command takes either a message ID or amessage number as an argument. For example:</p><blockquote><pre class="code">article 4Path: newshost.mydomain.com!news-w.ans.net!newsfeeds.ans.net!philabs ...From: user@mydomain.com (User Name)Newsgroups: local.testSubject: Is anybody there?Date: 21 Apr 1997...</pre></blockquote><p>If the <em class="emphasis">article</em> command is successful, the serverreturns the selected message.</p><p><a name="INDEX-2203" /></a><a name="INDEX-2204" /></a>The <em class="emphasis">xhdr</em> commandreturns the selected headers("X-headers") from articles in acertain newsgroup. The X-headers contain all the information aboutthe news article, including the poster's emailaddress, the subject of the message, the date and time the messagewas posted, the newsgroup(s) the message appears in, and the messageID. For example, if you want to see the subjects of the articles inthe <em class="emphasis">local.test</em> newsgroup, you can run<tt class="literal">xhdr subject 1-4</tt>, as follows:</p><blockquote><pre class="code">xhdr subject 1-4221 subject fields follow1   Is anybody there?2   Re: Is anybody there?3   Re: Is anybody there?4   Get a life!</pre></blockquote><p>The <tt class="literal">221</tt> status code indicates that the<em class="emphasis">xhdr</em> completed successfully and returned therequested headers. A Usenet client might display these headers to theuser, who can then select the articles to read based on the subjectlines.</p><p><a name="INDEX-2205" /></a>Acommand similar to <em class="emphasis">xhdr</em> is<em class="emphasis">xpat</em>, which matches X-headers against a patternstring you give it. For example, executing <tt class="literal">xpat subject 1-9*anybody*</tt> for <em class="emphasis">local.test</em> might looklike this:</p><blockquote><pre class="code">xpat subject 1-9 *anybody*221 subject matches follow.1   Is anybody there?2   Re: Is anybody there?3   Re: Is anybody there?</pre></blockquote><p><a name="INDEX-2206" /></a><a name="INDEX-2207" /></a>You can also post news articles withNNTP. When you issue the <em class="emphasis">post</em> command, theserver returns a code of <tt class="literal">340</tt> if posting ispermitted. If you get an error, such as <tt class="literal">501</tt>, youcannot post articles with this command. Most servers require that youinclude <tt class="literal">Newsgroups</tt>, <tt class="literal">Subject</tt>,and <tt class="literal">From</tt> headers. You will receive a<tt class="literal">240</tt> reply if your message has been successfullyposted. Here's an example of posting to<em class="emphasis">local.test</em> using NNTP:</p><blockquote><pre class="code">200 news.mydomain.com InterNetNews NNRP server INN 1.5.1 17-Dec-1996 ready(posting ok).post340 OkNewsgroups: local.testSubject: talking NNTPFrom: user@mydomain.comThis is a test posting.       .240 Article postedQUIT205 .</pre></blockquote><p>The server returns a code of <tt class="literal">205</tt> upondisconnecting. When the article arrives in<em class="emphasis">local.test</em>, it looks like this:</p><blockquote><pre class="code">From news.mydomain.com!not-for-mail Sun Mar  9 19:18:37 1997Path: news.mydomain.com!not-for-mailFrom: user@mydomain.comNewsgroups: local.testSubject: talking NNTPDate: 10 Mar 1997 00:09:33 GMTOrganization: Perl in a NutshellLines: 2Distribution: localMessage-ID: &lt;5fvjft$k3$2@news.mydomain.com&gt;NNTP-Posting-Host: newshost.mydomain.comXref: newshost.mydomain.com local.test:6This is a test posting.</pre></blockquote></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch16_02.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="ch17_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">16.2. The Mail Modules</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">17.2. Net::NNTP</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 + -