用 Perl 编写的 FTP 应用程序无法连接
为什么我的程序不能运行?它拒绝连接到主机,我尝试了两个不同的服务器并验证了使用哪个端口。 请注意,我对 Perl 的经验不是很丰富。
use strict;
use Net::FTP;
use warnings;
my $num_args = $#ARGV+1;
my $filename;
my $port;
my $host;
my $ftp;
if($num_args < 2)
{
print "Usage: ftp.pl host [port] file\n";
exit();
}
elsif($num_args == 3)
{
$port = $ARGV[1];
$host = $ARGV[0];
$filename = $ARGV[2];
print "Connecting to $host on port $port.\n";
$ftp = Net::FTP->new($host, Port => $port, Timeout => 30, Debug => 1)
or die "Can't open $host on port $port.\n";
}
else
{
$host = $ARGV[0];
$filename = $ARGV[1];
print "Connecting to $host with the default port.\n";
$ftp = Net::FTP->new($host, Timeout => 30, Debug => 1)
or die "Can't open $host on port $port.\n";
}
print "Usename: ";
my $username = <>;
print "\nPassword: ";
my $password = <>;
$ftp->login($username, $password);
$ftp->put($filename) or die "Can't upload $filename.\n";
print "Done!\n";
$ftp->quit;
提前致谢。
Why doesn't my program work? It refuses to connect to the host, I've tried two different servers and verified which port is used.
Note that I'm not very experienced when it comes to Perl.
use strict;
use Net::FTP;
use warnings;
my $num_args = $#ARGV+1;
my $filename;
my $port;
my $host;
my $ftp;
if($num_args < 2)
{
print "Usage: ftp.pl host [port] file\n";
exit();
}
elsif($num_args == 3)
{
$port = $ARGV[1];
$host = $ARGV[0];
$filename = $ARGV[2];
print "Connecting to $host on port $port.\n";
$ftp = Net::FTP->new($host, Port => $port, Timeout => 30, Debug => 1)
or die "Can't open $host on port $port.\n";
}
else
{
$host = $ARGV[0];
$filename = $ARGV[1];
print "Connecting to $host with the default port.\n";
$ftp = Net::FTP->new($host, Timeout => 30, Debug => 1)
or die "Can't open $host on port $port.\n";
}
print "Usename: ";
my $username = <>;
print "\nPassword: ";
my $password = <>;
$ftp->login($username, $password);
$ftp->put($filename) or die "Can't upload $filename.\n";
print "Done!\n";
$ftp->quit;
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在您已经有了答案
<>
->
,我想我看到了问题。当@ARGV
包含任何内容时,<>
就是“魔法打开”。 Perl 将@ARGV
中的下一项解释为文件名,打开它并逐行读取它。因此,我认为您可能可以这样做:然后,如果您在文件(例如名为 cred)中有一些连接信用
,那么
将使用 cred 中的凭据打开文件的 host:8020 。
我不确定你想这样做,这就是
<>
的工作原理。Now that you already have your answer
<>
-><STDIN>
, I think I see the problem. When@ARGV
contains anything,<>
is the 'magic open'. Perl interprets the next item in@ARGV
as a filename, opens it and reads it line by line. Therefore, I think you can probably do something like:Then if you had some connection creditials in a file (say named cred) like
then
would open host:8020 for file using credentials in cred.
I'm not sure you want to do that, its just that THAT is how
<>
works.