📄 ch18_02.htm
字号:
<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 & 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->new($hostname); # Construct object$ftp->login($username, $password); # Log in$ftp->cwd($home),"\n"; # Change directoryprint $ftp->ls($home),"\n"; # Now get the file and leave$ftp->get($filename); $ftp->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->new($hostname); # Net::FTP constructor$ftp->login($username, $password); # Log in w/username and password$pwd = $ftp->pwd; # Get current directoryprint <<HTML; # Output HTML pageContent-type: text/html<HTML> <HEAD> <TITLE>Download Files</TITLE> </HEAD> <BODY> <B>Current working directory:</B> $pwd<BR> Files to download: <P>HTML @entries = $ftp->ls($home); # Slurp all entries into an array foreach (@entries) { # Output links for all files in the ftp area # as links print "<INPUT TYPE=hidden NAME=\"files\" VALUE=\"$_\">\n"; print "<A HREF=\"ftp://$hostname$_\">", "<IMG SRC=\"http://www/icons/f.gif\" border=0>\n"; print " $_</A><BR>\n"; } print <<HTML; </BODY></HTML>HTML$ftp->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->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>->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>->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>->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 + -