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

📄 ch18_02.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<html><head><title>Net::FTP (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="ch18_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="ch18_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">18.2. Net::FTP</h2><p><a name="INDEX-2285" /><a name="INDEX-2286" />Net::FTP is used to transfer filesfrom remote hosts. With Net::FTP, you can write simple FTP clientsthat transfer files from remote servers based on information passedon the command line or from hardcoded variables. Here is an exampleof a client that connects to a remote FTP server and gets a file fromthe server:</p><blockquote><pre class="code">#!/usr/local/bin/perl -wuse Net::FTP;$hostname = 'remotehost.com';$username = 'anonymous';$password = 'myname@mydomain.com';# Hardcode the directory and filename to get$home = '/pub';$filename = 'TESTFILE';# Open the connection to the host$ftp = Net::FTP-&gt;new($hostname);        # Construct object$ftp-&gt;login($username, $password);      # Log in$ftp-&gt;cwd($home),"\n";                  # Change directoryprint $ftp-&gt;ls($home),"\n";        # Now get the file and leave$ftp-&gt;get($filename); $ftp-&gt;quit;</pre></blockquote><p>FTP clients have also been integrated with most World Wide Webbrowsers, using <em class="emphasis">ftp://</em> in place of <em class="emphasis">http://</em>. When the URL points to a directory,the browser displays a listing of the directory, in which eachfilename is a link to that file. When the URL points directly to afile, the remote file is downloaded.</p><p>Here's an example that uses Net::FTP to list filesfrom a remote FTP server on a web page, with a link from each file tothe URL of the file on the remote site:</p><blockquote><pre class="code">#!/usr/local/bin/perl -wuse Net::FTP;$hostname = 'remotehost.com';        # FTP host$username = 'anonymous';             # Username$password = 'myname@mydomain.com';   # Password$home = '/pub';$ftp = Net::FTP-&gt;new($hostname);     # Net::FTP constructor$ftp-&gt;login($username, $password);  # Log in w/username and password$pwd = $ftp-&gt;pwd;                   # Get current directoryprint &lt;&lt;HTML;                       # Output HTML pageContent-type: text/html&lt;HTML&gt;    &lt;HEAD&gt;    &lt;TITLE&gt;Download Files&lt;/TITLE&gt;  &lt;/HEAD&gt;  &lt;BODY&gt;            &lt;B&gt;Current working directory:&lt;/B&gt; $pwd&lt;BR&gt;      Files to download: &lt;P&gt;HTML      @entries = $ftp-&gt;ls($home);     # Slurp all entries into an array    foreach (@entries) { # Output links for all files in the ftp area                         # as links        print "&lt;INPUT TYPE=hidden NAME=\"files\" VALUE=\"$_\"&gt;\n";        print "&lt;A HREF=\"ftp://$hostname$_\"&gt;",        "&lt;IMG SRC=\"http://www/icons/f.gif\" border=0&gt;\n";        print " $_&lt;/A&gt;&lt;BR&gt;\n";    }    print &lt;&lt;HTML;  &lt;/BODY&gt;&lt;/HTML&gt;HTML$ftp-&gt;quit;             # end FTP session</pre></blockquote><p>The Net::FTP module implements a subset (as shown earlier in thischapter) of the FTP protocol as defined in RFC 959. In addition toproviding the methods shown below, the module inherits from Net::Cmd.Some of the Net::FTP methods return an object derived from the<em class="emphasis">dataconn</em> class (which is in turn derived fromthe IO::Socket::INET class), as noted in the entries for thosemethods.</p><p>The following methods are defined by Net::FTP.</p><a name="INDEX-2287" /><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">ftp</em> = Net::FTP-&gt;new(<em class="replaceable">host</em>[, <em class="replaceable">options</em>])</pre><p><a name="INDEX-2287" />Constructs a new Net::FTP object.Arguments are:</p><dl><dt><i><em class="replaceable"><tt>host</tt></em></i></dt><dd>The name of the remote host</p></dd><dt><i><em class="replaceable"><tt>options</tt></em></i></dt><dd>A hash specifying any of the following information:</p><dl><dt><b><tt class="literal">Firewall</tt></b></dt><dd>Name of an FTP firewall.</p></dd><dt><b><tt class="literal">Port</tt></b></dt><dd>Port number to use for the FTP connection. Defaults to<tt class="literal">21</tt>.</p></dd><dt><b><tt class="literal">Timeout</tt></b></dt><dd>Timeout value. Defaults to 120 seconds.</p></dd><dt><b><tt class="literal">Debug</tt></b></dt><dd>Debug level.</p></dd><dt><b><tt class="literal">Passive</tt></b></dt><dd>True or false, specifying whether to perform transfers in passivemode.</p></dd></dl></dd></dl></div><a name="INDEX-2288" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>abort</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">ftp</em>-&gt;abort(  )</pre><p><a name="INDEX-2288" />Aborts the current transaction.</p></div><a name="INDEX-2289" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>appe</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">ftp</em>-&gt;appe(<em class="replaceable">file</em>)</pre><p><a name="INDEX-2289" />Appends data to the end of theremote file <em class="replaceable"><tt>file</tt></em>, which is created if itdoesn't exist. If the user calls either<tt class="literal">pasv</tt> or <tt class="literal">port</tt>, returns true orfalse. Otherwise, returns a reference to a Net::FTP::dataconn object.</p></div><a name="INDEX-2290" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>append</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">ftp</em>-&gt;append(<em class="replaceable">local</em>[, <em class="replaceable">remote</em>])</pre><p><a name="INDEX-2290" />Appends the contents of a local file toan existing file on the remote system. Arguments are:</p><dl><dt><i><em class="replaceable"><tt>local</tt></em></i></dt><dd>Either the name of the file on the local system to transfer or afilehandle.</p></dd><dt><i><em class="replaceable"><tt>remote</tt></em></i></dt><dd>

⌨️ 快捷键说明

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