0

出于测试目的,我需要编写一个程序,使用 [ Net::FTP][Net::FTP] 连接到服务器,然后接收某个目录中的文件。收到后,立即将其放回同一位置。

这是我的代码:

   #!/usr/bin/perl

   use Net::FTP;

   $host = "serverA";
   $username = "test";
   $password = "ftptest123";
   $ftpdir = "/ftptest";
   $file = "ftptest.txt";

   $ftp = Net::FTP->new($host) or die "Error connecting to $host: $!";

   $ftp->login($username,$password) or die "Login failed: $!";

   $ftp->cwd($ftpdir) or die "Can't go to $ftpdir: $!";

   $ftp->get($file) or die "Can't get $file: $!";

   $ftp->put($file) or die "Can't put $file: $!";

   $ftp->quit or die "Error closing ftp connection: $!";

关于如何解决这个问题的任何想法?它似乎运行良好,但是当它遇到put语句时,它会向我射击:

[Net::FTP]: https://metacpan.org/module/Net::FTP
4

2 回答 2

1

检查错误消息$ftp->message,而不是$!。它可能会告诉您您没有对该目录的写入访问权限,或者不允许覆盖现有文件...

于 2012-12-03T23:07:28.793 回答
1

首先,您应该始终 use strictand use warnings,并在所有变量的第一个使用点使用my. 这样一来,许多您可能会忽略的琐碎错误都会为您突出显示。

的文档Net::FTP不完整,因为它没有提供有关该message方法的任何信息。但是,从概要中可以清楚地看出,任何错误的信息都可以使用$ftp->message.

当然这不适用于构造函数,好像没有对象提供message方法一样失败,所以在这种情况下,信息出现在内置变量中$@

在您的程序上尝试这种变化。它可能会立即告诉您失败的原因。

#!/usr/bin/perl

use strict;
use warnings;

use Net::FTP;

my $host = 'serverA';
my $username = 'test';
my $password = 'ftptest123';
my $ftpdir = '/ftptest';
my $file = 'ftptest.txt';

my $ftp = Net::FTP->new($host) or die "Error connecting to $host: $@";

$ftp->login($username,$password) or die "Login failed: ", $ftp->message;

$ftp->cwd($ftpdir) or die "Can't go to $ftpdir: ", $ftp->message;

$ftp->get($file) or die "Can't get $file: ", $ftp->message;

$ftp->put($file) or die "Can't put $file: ", $ftp->message;

$ftp->quit or die "Error closing ftp connection: ", $ftp->message;
于 2012-12-04T04:12:05.653 回答