libnetfaq.html

来自「perl教程」· HTML 代码 · 共 420 行 · 第 1/2 页

HTML
420
字号
<pre>
      <span class="variable">$ftp</span><span class="operator">-&gt;</span><span class="variable">get</span><span class="operator">(</span><span class="variable">$name</span><span class="operator">,</span><span class="variable">$name</span><span class="operator">);</span>
          <span class="operator">}</span>
</pre>
<pre>
    <span class="comment"># ftp done!</span>
</pre>
<pre>
    <span class="variable">$ftp</span><span class="operator">-&gt;</span><span class="variable">quit</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="how_do_i_transfer_files_in_binary_mode_">How do I transfer files in binary mode ?</a></h2>
<p>To transfer files without &lt;LF&gt;&lt;CR&gt; translation Net::FTP provides
the <code>binary</code> method</p>
<pre>
    <span class="variable">$ftp</span><span class="operator">-&gt;</span><span class="variable">binary</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="how_can_i_get_the_size_of_a_file_on_a_remote_ftp_server_">How can I get the size of a file on a remote FTP server ?</a></h2>
<p>
</p>
<h2><a name="how_can_i_get_the_modification_time_of_a_file_on_a_remote_ftp_server_">How can I get the modification time of a file on a remote FTP server ?</a></h2>
<p>
</p>
<h2><a name="how_can_i_change_the_permissions_of_a_file_on_a_remote_server_">How can I change the permissions of a file on a remote server ?</a></h2>
<p>The FTP protocol does not have a command for changing the permissions
of a file on the remote server. But some ftp servers may allow a chmod
command to be issued via a SITE command, eg</p>
<pre>
    <span class="variable">$ftp</span><span class="operator">-&gt;</span><span class="variable">quot</span><span class="operator">(</span><span class="string">'site chmod 0777'</span><span class="operator">,</span><span class="variable">$filename</span><span class="operator">);</span>
</pre>
<p>But this is not guaranteed to work.</p>
<p>
</p>
<h2><a name="can_i_do_a_reget_operation_like_the_ftp_command_">Can I do a reget operation like the ftp command ?</a></h2>
<p>
</p>
<h2><a name="how_do_i_get_a_directory_listing_from_an_ftp_server_">How do I get a directory listing from an FTP server ?</a></h2>
<p>
</p>
<h2><a name="changing_directory_to__does_not_fail_">Changing directory to &quot;&quot; does not fail ?</a></h2>
<p>Passing an argument of &quot;&quot; to -&gt;<code>cwd()</code> has the same affect of calling -&gt;<code>cwd()</code>
without any arguments. Turn on Debug (<em>See below</em>) and you will see what is
happening</p>
<pre>
    <span class="variable">$ftp</span> <span class="operator">=</span> <span class="variable">Net::FTP</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(</span><span class="variable">$host</span><span class="operator">,</span> <span class="string">Debug</span> <span class="operator">=&gt;</span> <span class="number">1</span><span class="operator">);</span>
    <span class="variable">$ftp</span><span class="operator">-&gt;</span><span class="variable">login</span><span class="operator">;</span>
    <span class="variable">$ftp</span><span class="operator">-&gt;</span><span class="variable">cwd</span><span class="operator">(</span><span class="string">""</span><span class="operator">);</span>
</pre>
<p>gives</p>
<pre>
    Net::FTP=GLOB(0x82196d8)&gt;&gt;&gt; CWD /
    Net::FTP=GLOB(0x82196d8)&lt;&lt;&lt; 250 CWD command successful.</pre>
<p>
</p>
<h2><a name="i_am_behind_a_socks_firewall__but_the_firewall_option_does_not_work_">I am behind a SOCKS firewall, but the Firewall option does not work ?</a></h2>
<p>The Firewall option is only for support of one type of firewall. The type
supported is an ftp proxy.</p>
<p>To use Net::FTP, or any other module in the libnet distribution,
through a SOCKS firewall you must create a socks-ified perl executable
by compiling perl with the socks library.</p>
<p>
</p>
<h2><a name="i_am_behind_an_ftp_proxy_firewall__but_cannot_access_machines_outside_">I am behind an FTP proxy firewall, but cannot access machines outside ?</a></h2>
<p>Net::FTP implements the most popular ftp proxy firewall approach. The scheme
implemented is that where you log in to the firewall with <code>user@hostname</code></p>
<p>I have heard of one other type of firewall which requires a login to the
firewall with an account, then a second login with <code>user@hostname</code>. You can
still use Net::FTP to traverse these firewalls, but a more manual approach
must be taken, eg</p>
<pre>
    <span class="variable">$ftp</span> <span class="operator">=</span> <span class="variable">Net::FTP</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(</span><span class="variable">$firewall</span><span class="operator">)</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="variable">$@</span><span class="operator">;</span>
    <span class="variable">$ftp</span><span class="operator">-&gt;</span><span class="variable">login</span><span class="operator">(</span><span class="variable">$firewall_user</span><span class="operator">,</span> <span class="variable">$firewall_passwd</span><span class="operator">)</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="variable">$ftp</span><span class="operator">-&gt;</span><span class="variable">message</span><span class="operator">;</span>
    <span class="variable">$ftp</span><span class="operator">-&gt;</span><span class="variable">login</span><span class="operator">(</span><span class="variable">$ext_user</span> <span class="operator">.</span> <span class="string">'@'</span> <span class="operator">.</span> <span class="variable">$ext_host</span><span class="operator">,</span> <span class="variable">$ext_passwd</span><span class="operator">)</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="variable">$ftp</span><span class="operator">-&gt;</span><span class="variable">message</span><span class="operator">.</span>
</pre>
<p>
</p>
<h2><a name="my_ftp_proxy_firewall_does_not_listen_on_port_21">My ftp proxy firewall does not listen on port 21</a></h2>
<p>FTP servers usually listen on the same port number, port 21, as any other
FTP server. But there is no reason why this has to be the case.</p>
<p>If you pass a port number to Net::FTP then it assumes this is the port
number of the final destination. By default Net::FTP will always try
to connect to the firewall on port 21.</p>
<p>Net::FTP uses IO::Socket to open the connection and IO::Socket allows
the port number to be specified as part of the hostname. So this problem
can be resolved by either passing a Firewall option like <code>&quot;hostname:1234&quot;</code>
or by setting the <code>ftp_firewall</code> option in Net::Config to be a string
in in the same form.</p>
<p>
</p>
<h2><a name="is_it_possible_to_change_the_file_permissions_of_a_file_on_an_ftp_server_">Is it possible to change the file permissions of a file on an FTP server ?</a></h2>
<p>The answer to this is &quot;maybe&quot;. The FTP protocol does not specify a command to change
file permissions on a remote host. However many servers do allow you to run the
chmod command via the <code>SITE</code> command. This can be done with</p>
<pre>
  <span class="variable">$ftp</span><span class="operator">-&gt;</span><span class="variable">site</span><span class="operator">(</span><span class="string">'chmod'</span><span class="operator">,</span><span class="string">'0775'</span><span class="operator">,</span><span class="variable">$file</span><span class="operator">);</span>
</pre>
<p>
</p>
<h2><a name="i_have_seen_scripts_call_a_method_message__but_cannot_find_it_documented_">I have seen scripts call a method message, but cannot find it documented ?</a></h2>
<p>Net::FTP, like several other packages in libnet, inherits from Net::Cmd, so
all the methods described in Net::Cmd are also available on Net::FTP
objects.</p>
<p>
</p>
<h2><a name="why_does_net__ftp_not_implement_mput_and_mget_methods">Why does Net::FTP not implement mput and mget methods</a></h2>
<p>The quick answer is because they are easy to implement yourself. The long
answer is that to write these in such a way that multiple platforms are
supported correctly would just require too much code. Below are
some examples how you can implement these yourself.</p>
<p>sub mput {
  <a href="../../lib/Pod/perlfunc.html#item_my"><code>my($ftp,$pattern)</code></a> = @_;
  foreach my $file (glob($pattern)) {
    $ftp-&gt;<code>put($file)</code> or warn $ftp-&gt;message;
  }
}</p>
<p>sub mget {
  <a href="../../lib/Pod/perlfunc.html#item_my"><code>my($ftp,$pattern)</code></a> = @_;
  foreach my $file ($ftp-&gt;<code>ls($pattern))</code> {
    $ftp-&gt;<code>get($file)</code> or warn $ftp-&gt;message;
  }
}</p>
<p>
</p>
<hr />
<h1><a name="using_net__smtp">Using Net::SMTP</a></h1>
<p>
</p>
<h2><a name="why_can_t_the_part_of_an_email_address_after_the___be_used_as_the_hostname_">Why can't the part of an Email address after the @ be used as the hostname ?</a></h2>
<p>The part of an Email address which follows the @ is not necessarily a hostname,
it is a mail domain. To find the name of a host to connect for a mail domain
you need to do a DNS MX lookup</p>
<p>
</p>
<h2><a name="why_does_net__smtp_not_do_dns_mx_lookups_">Why does Net::SMTP not do DNS MX lookups ?</a></h2>
<p>Net::SMTP implements the SMTP protocol. The DNS MX lookup is not part
of this protocol.</p>
<p>
</p>
<h2><a name="the_verify_method_always_returns_true_">The verify method always returns true ?</a></h2>
<p>Well it may seem that way, but it does not. The verify method returns true
if the command succeeded. If you pass verify an address which the
server would normally have to forward to another machine, the command
will succeed with something like</p>
<pre>
    252 Couldn't verify &lt;someone@there&gt; but will attempt delivery anyway</pre>
<p>This command will fail only if you pass it an address in a domain
the server directly delivers for, and that address does not exist.</p>
<p>
</p>
<hr />
<h1><a name="debugging_scripts">Debugging scripts</a></h1>
<p>
</p>
<h2><a name="how_can_i_debug_my_scripts_that_use_net____modules_">How can I debug my scripts that use Net::* modules ?</a></h2>
<p>Most of the libnet client classes allow options to be passed to the
constructor, in most cases one option is called <code>Debug</code>. Passing
this option with a non-zero value will turn on a protocol trace, which
will be sent to STDERR. This trace can be useful to see what commands
are being sent to the remote server and what responses are being
received back.</p>
<pre>
    <span class="comment">#!/your/path/to/perl</span>
</pre>
<pre>
    <span class="keyword">use</span> <span class="variable">Net::FTP</span><span class="operator">;</span>
</pre>
<pre>
    <span class="keyword">my</span> <span class="variable">$ftp</span> <span class="operator">=</span> <span class="variable">new</span> <span class="variable">Net::FTP</span><span class="operator">(</span><span class="variable">$host</span><span class="operator">,</span> <span class="string">Debug</span> <span class="operator">=&gt;</span> <span class="number">1</span><span class="operator">);</span>
    <span class="variable">$ftp</span><span class="operator">-&gt;</span><span class="variable">login</span><span class="operator">(</span><span class="string">'gbarr'</span><span class="operator">,</span><span class="string">'password'</span><span class="operator">);</span>
    <span class="variable">$ftp</span><span class="operator">-&gt;</span><span class="variable">quit</span><span class="operator">;</span>
</pre>
<p>this script would output something like</p>
<pre>
 Net::FTP: Net::FTP(2.22)
 Net::FTP:   Exporter
 Net::FTP:   Net::Cmd(2.0801)
 Net::FTP:   IO::Socket::INET
 Net::FTP:     IO::Socket(1.1603)
 Net::FTP:       IO::Handle(1.1504)</pre>
<pre>
 Net::FTP=GLOB(0x8152974)&lt;&lt;&lt; 220 imagine FTP server (Version wu-2.4(5) Tue Jul 29 11:17:18 CDT 1997) ready.
 Net::FTP=GLOB(0x8152974)&gt;&gt;&gt; user gbarr
 Net::FTP=GLOB(0x8152974)&lt;&lt;&lt; 331 Password required for gbarr.
 Net::FTP=GLOB(0x8152974)&gt;&gt;&gt; PASS ....
 Net::FTP=GLOB(0x8152974)&lt;&lt;&lt; 230 User gbarr logged in.  Access restrictions apply.
 Net::FTP=GLOB(0x8152974)&gt;&gt;&gt; QUIT
 Net::FTP=GLOB(0x8152974)&lt;&lt;&lt; 221 Goodbye.</pre>
<p>The first few lines tell you the modules that Net::FTP uses and their versions,
this is useful data to me when a user reports a bug. The last seven lines
show the communication with the server. Each line has three parts. The first
part is the object itself, this is useful for separating the output
if you are using multiple objects. The second part is either <code>&lt;&lt;&lt;&lt;</code> to
show data coming from the server or <code>&amp;gt&amp;gt&amp;gt&amp;gt</code> to show data
going to the server. The remainder of the line is the command
being sent or response being received.</p>
<p>
</p>
<hr />
<h1><a name="author_and_copyright">AUTHOR AND COPYRIGHT</a></h1>
<p>Copyright (c) 1997 Graham Barr.
All rights reserved.</p>
<hr><p><em>$Id: //depot/libnet/Net/libnetFAQ.pod#6 $</em></p>

</body>

</html>

⌨️ 快捷键说明

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