Python and Perl ftp Download Tutorial

Python and Perl ftp Download Tutorial

Python and Perl ftp Download Tutorial

The Three Ps ride again today, building on yesterday’s Perl ftp and the Three Ps Tutorial and adding in the ability to download a single file at a time as a “Download” scenario … in modern parlance … though in “ftp” parlance it is more the go to think “GET” or “RETR”. Which brings up the point that “ftp” is a very old protocol by now, often replaced in people’s minds by the more secure “sftp” protocol, and a myriad of other ways, these days, we can share files and data. Be that as it may, once you “get” (chortle, chortle) the ideas of “ftp”, it stands to reason, that you are “walking in the same footsteps” of many many I.T. types out there … who would blush, remembering “Kermit”, for instance.

This “ftp” woooooorrrrlllldd was a very command line thing, at least for me, in that I found it infinitely useful to “plonk” into procedures of some complexity, that would link data with processes in that “input/output centred approach” of the “old days”. “Old days” “smold days” … data is still I.T.! Front ends like FileZilla and many others, make it much more user friendly, of course, and less error prone, especially for those of us bamboozled by the “binary” versus “text” modes of transfer … and which I just decided to go all out for “bamboo” everything … thanks.

The “ftp” “get” smarts for Python and Perl just function, today, with the simple idea that the user flags this intention, by adding another command line argument. We, in the Python and Perl code, do some checks about …

  • in our code, a single file download (the only approach we accept) must involve a filename with a dot (“.”)
  • intended downloads that would clobber a file of the same name … which we do not encourage in our very simple code today

That’s what programming’s like. Even simple ideas get complex when you think about things a little, and direct your mind towards “implications”, and in that line of thinking, security wise, of course, we discourage all of this “download functionality” on the rjmprogramming.com.au domain live run but rather direct you towards downloading your own …

… onto your own local web server system, such as MAMP, to really test this and nuance it for yourself.


Previous relevant Perl ftp and the Three Ps Tutorial is shown below.

Perl ftp and the Three Ps Tutorial

Perl ftp and the Three Ps Tutorial

Yesterday’s Python ftp Primer Tutorial is given a “return of the Three Ps” feel today. Yes, we code some Perl to do similar ftp listing logic as we did with Python yesterday, so that …


#!/usr/bin/perl
# ftp_url.pl
# Ftp URL listing
# RJM Programming
# November, 2017
# Thanks to https://www.foo.be/docs/tpj/issues/vol1_3/tpj0103-0007.html

use Net::FTP;
#use File::Listing qw(parse_dir);

# Create a new connection to the ftp server
sub connection { # Create a new NET::FTP object
$ftp = Net::FTP->new($CPANhost, Timeout => 60)
or die "Can't contact $CPANhost: $!";
# We shall login to the ftp server as anonymous;
$ftp->login($CPANusername,$CPANpassword)
or die "Can't login ($CPANhost):" .
$ftp->message;
# Change the working directory
$ftp->cwd($CPANpath) or die
"Can't change directory ($CPANhost):".
$ftp->message;
return $ftp;
}


# The path to the CPAN/modules directory on most CPAN hosts
$CPANpath = '/';

$CPANusername = 'mkuulma@ozemail.com.au';
$CPANpassword = '';

# Change this to the name of your nearest CPAN host
$CPANhost = '';
my $size = @ARGV;
if ($size == 2) {
$CPANhost = 'ftp.ozemail.com.au';
$CPANpath = $ARGV[1];
$CPANpassword = $ARGV[0];
} else {
if ($size == 1) {
$CPANhost = 'ftp.ozemail.com.au';
$CPANpassword = $ARGV[0];
} else {
if ($size == 3) {
$CPANhost = $ARGV[0];
$CPANusername = $ARGV[1];
$CPANpassword = $ARGV[2];
} else {
if ($size == 4) {
$CPANhost = $ARGV[0];
$CPANusername = $ARGV[1];
$CPANpassword = $ARGV[2];
$CPANpath = $ARGV[3];
} else {
print "Usage is via ... perl ftp_url.pl [ftphost=ftp.ozemail.com.au] [ftpusername=mkuulma@ozemail.com.au] ftppassword [subpath=/]\n";
}
}
}
}

if ($CPANpassword eq '') {
$CPANpassword = '';
} else {
# Create the initial connection
$ftp = connection();

$ftp->pasv(); # passive mode

# Retrieve a recursive directory listing
my @ls = $ftp->ls('-l');

for my $filex (@ls) {
my $file = $filex;
my (@description, $size);
if (-e $file) {
push @description, 'binary' if (-B _);
push @description, 'a socket' if (-S _);
push @description, 'a text file' if (-T _);
push @description, 'a block special file' if (-b _);
push @description, 'a character special file' if (-c _);
push @description, 'a directory' if (-d _);
push @description, 'executable' if (-x _);
push @description, (($size = -s _)) ? "$size bytes" : 'empty';
push @description, (($daysago = -M _)) ? " modified $daysago days ago" : '';
print "\n\t$file is ", join(', ',@description),"";
}
}
print "\n\t";
}

… or you can download it as ftp_url.pl (supervised by ftp_url.php changed in this way, where you may notice multiple HTML input type=submit buttons used to differentiate the two strands of ftp functionality modes).

With this Perl coding we again use command line arguments as the technique to handle user defined data to define the ftp connection.

Think server side languages, think “The Three Ps”!


Previous relevant Python ftp Primer Tutorial is shown below.

Python ftp Primer Tutorial

Python ftp Primer Tutorial

Python is a great server side language to learn. Some web server configurations use it as their “go to” server language, though ours here at rjmprogramming.com.au uses PHP (and so we’ve written a PHP “supervisor” to dovetail with today’s Python code).

We’re going to write this Python code, that facilitates the listing of an ftp URL “path”, to run as if from a Linux (or Mac OS X, which we tested with for the work today) command line.

So, underlying the PHP supervisory live run today, the hard working “duck child” Python goes like …


"""
ftp_url.py - RJM Programming - November 2017
Show some ftp functionality ... thanks to Python & XML by O'Reilly ISBN: 0-596-00128-2
"""
import sys

from urllib import urlopen

def main(argv):

if len(sys.argv) == 5:
fd=urlopen("ftp://" + sys.argv[2] + ":" + sys.argv[3] + "@" + sys.argv[1] + sys.argv[4])
else:
if len(sys.argv) == 4:
fd=urlopen("ftp://" + sys.argv[2] + ":" + sys.argv[3] + "@" + sys.argv[1])
else:
if len(sys.argv) == 3:
fd=urlopen("ftp://mkuulma@ozemail.com.au:" + sys.argv[1] + "@ftp.ozemail.com.au" + sys.argv[2])
else:
if len(sys.argv) == 2:
fd=urlopen("ftp://mkuulma@ozemail.com.au:" + sys.argv[1] + "@ftp.ozemail.com.au")
else:
print "Usage is via ... python ftp_url.py [ftphost=ftp.ozemail.com.au] [ftpusername=mkuulma@ozemail.com.au] ftppassword [subpath=/]"
exit()

print fd.read()
exit()

if __name__ == "__main__":
main(sys.argv[1:])

… or you can download it as ftp_url.py (supervised by ftp_url.php).

Interesting, huh?!

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Tutorials and tagged , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>