FTP: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: re add directory change command)
m (→‎{{header|Perl}}: use a working server, cleaner code)
Line 529: Line 529:


=={{header|Perl}}==
=={{header|Perl}}==
<lang Perl>
<lang perl>use Net::FTP;
#!/usr/bin/perl
use strict;
use warnings;
use 5.020;
#This script is dependent upon the Net::FTP cpan module
use Net::FTP;


# set server and credentials
#Put the name of the FTP server here
my $host = "kernel.org";
my $host = 'speedtest.tele2.net';
my $user = 'anonymous';
#Credentials go here
my $user = "anonymous";
my $password = '';
my $password = "";


# connect in passive mode
#Attempt to connect to the server using the credentials provided.
my $f = Net::FTP->new($host) or die("Something went wrong. Can't open $host\n");
my $f = Net::FTP->new($host) or die "Can't open $host\n";
$f->login($user, $password) or die("Something went wrong. Can't log $user in.\n");
$f->login($user, $password) or die "Can't login as $user\n";

#Set to passive mode
$f->passive();
$f->passive();


# change remote directory, list contents
#Change to whatever directory you want. If no args are passed to cwd(), it sets it to the root directory
$f->cwd("pub/linux/kernel");
$f->cwd('upload');
@files = $f->ls();
#Print the current dir
printf "Currently %d files in the 'upload' directory.\n", @files;
my @dir = $f->ls();
foreach my $element (@dir)
{
say("$element");
}

#Download the file and store locally. get() returns the local filename
my $local = $f->get("README");
say("Your file was stored as $local in the current directory! ");
</lang>


# download file in binary mode
Output:
$f->cwd('/');
<pre>
$f->type('binary');
COPYING
$local = $f->get('512KB.zip');
CREDITS
print "Your file was stored as $local in the current directory\n";</lang>
Historic
{{out}}
README
<pre>Currently 20 files in the 'upload' directory
SillySounds
Your file was stored as 512KB.zip in the current directory!</pre>
crypto
next
people
ports
projects
sha256sums.asc
testing
uemacs
v1.0
v1.1
v1.2
v1.3
v2.0
v2.1
v2.2
v2.3
v2.4
v2.5
v2.6
v3.0
v3.x
v4.x
Your file was stored as README in the current directory!
</pre>


=={{header|Perl 6}}==
=={{header|Perl 6}}==