{"id":34513,"date":"2017-11-27T03:01:50","date_gmt":"2017-11-26T17:01:50","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=34513"},"modified":"2017-11-27T05:08:49","modified_gmt":"2017-11-26T19:08:49","slug":"python-and-perl-ftp-download-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/python-and-perl-ftp-download-tutorial\/","title":{"rendered":"Python and Perl ftp Download Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Python and Perl ftp Download Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url_3ps_download.jpg\" title=\"Python and Perl ftp Download Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Python and Perl ftp Download Tutorial<\/p><\/div>\n<p>The Three Ps ride again today, building on yesterday&#8217;s <a title='Perl ftp and the Three Ps Tutorial' href='#ptpt'>Perl ftp and the Three Ps Tutorial<\/a> and adding in the ability to download a single file at a time as a &#8220;Download&#8221; scenario &#8230; in modern parlance &#8230; though in <a target=_blank title='File Transfer Protocol information from Wikipedia ... thanks' href='https:\/\/en.wikipedia.org\/wiki\/File_Transfer_Protocol'>&#8220;ftp&#8221;<\/a> parlance it is more the go to think &#8220;GET&#8221; or &#8220;RETR&#8221;.  Which brings up the point that &#8220;ftp&#8221; is a very old protocol by now, often replaced in people&#8217;s minds by the more secure &#8220;sftp&#8221; protocol, and a myriad of other ways, these days, we can share files and data.   Be that as it may, once you &#8220;get&#8221; (chortle, chortle) the ideas of &#8220;ftp&#8221;, it stands to reason, that you are &#8220;walking in the same footsteps&#8221; of many many I.T. types out there &#8230; who would blush, remembering <a target=_blank title='Kermit protocol information from Wikipedia ... thanks' href='https:\/\/en.wikipedia.org\/wiki\/Kermit_(protocol)'>&#8220;Kermit&#8221;<\/a>, for instance.<\/p>\n<p>This &#8220;ftp&#8221; woooooorrrrlllldd was a very command line thing, at least for me, in that I found it infinitely useful to &#8220;plonk&#8221; into procedures of some complexity, that would link data with processes in that &#8220;input\/output centred approach&#8221; of the &#8220;old days&#8221;.  &#8220;Old days&#8221; &#8220;smold days&#8221; &#8230; data is still I.T.!  Front ends like <a target=_blank title='FileZilla landing page from SourceForge ... thanks' href='https:\/\/filezilla-project.org\/'>FileZilla<\/a> and many others, make it much more user friendly, of course, and less error prone, especially for those of us bamboozled by the &#8220;binary&#8221; versus &#8220;text&#8221; modes of transfer &#8230; and which I just decided to go all out for &#8220;bamboo&#8221; everything &#8230; thanks.<\/p>\n<p>The &#8220;ftp&#8221; &#8220;get&#8221; 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 &#8230;<\/p>\n<ul>\n<li>in our code, a single file download (the only approach we accept) must involve a filename with a dot (&#8220;.&#8221;)<\/li>\n<li>intended downloads that would clobber a file of the same name &#8230; which we do not encourage in our very simple code today<\/li>\n<\/ul>\n<p>That&#8217;s what programming&#8217;s like.  Even simple ideas get complex when you think about things a little, and direct your mind towards &#8220;implications&#8221;, and in that line of thinking, security wise, of course, we discourage all of this &#8220;download functionality&#8221; on the rjmprogramming.com.au domain <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php\" title=\"Click picture\">live run<\/a> but rather direct you towards downloading your own &#8230;<\/p>\n<ul>\n<li><b>P<\/b>HP <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php--GETME\" title=\"ftp_url.php\">ftp_url.php<\/a> (changed in <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php--GETME\">this way<\/a>) &#8230; supervising &#8230;<\/li>\n<li><b>P<\/b>ython <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.py-GETME\" title=\"ftp_url.py\">ftp_url.py<\/a> (changed in <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.py-GETME\">this way<\/a>) &#8230; or &#8230;<\/li>\n<li><b>P<\/b>erl <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.pl-GETME\" title=\"ftp_url.pl\">ftp_url.pl<\/a> (changed in <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.pl-GETME\">this way<\/a>)<\/li>\n<\/ul>\n<p> &#8230; onto your own local web server system, such as <a target=_blank title='MAMP for Apache\/PHP\/MySql on Mac OS X local web server' href='http:\/\/www.mamp.info'>MAMP<\/a>, to really test this and nuance it for yourself.<\/p>\n<hr>\n<p id='ptpt'>Previous relevant <a target=_blank title='Perl ftp and the Three Ps Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/perl-ftp-and-the-three-ps-tutorial\/'>Perl ftp and the Three Ps Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Perl ftp and the Three Ps Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url_3ps.jpg\" title=\"Perl ftp and the Three Ps Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Perl ftp and the Three Ps Tutorial<\/p><\/div>\n<p>Yesterday&#8217;s <a title='Python ftp Primer Tutorial' href='#ppt'>Python ftp Primer Tutorial<\/a> is given a &#8220;return of the Three Ps&#8221; feel today.  Yes, we code some Perl to do similar ftp listing logic as we did with Python yesterday, so that &#8230;<\/p>\n<ul>\n<li><b>P<\/b>HP <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php\" title=\"Click picture\">supervises ftp listing functionality<\/a> provided by either &#8230;<\/li>\n<li><b>P<\/b>ython &#8230; or &#8230;<\/li>\n<li><b>P<\/b>erl &#8230; and this new Perl code is &#8230;<\/li>\n<\/ul>\n<p><code><br \/>\n#!\/usr\/bin\/perl<br \/>\n# ftp_url.pl<br \/>\n# Ftp URL listing<br \/>\n# RJM Programming<br \/>\n# November, 2017<br \/>\n# Thanks to https:\/\/www.foo.be\/docs\/tpj\/issues\/vol1_3\/tpj0103-0007.html<br \/>\n<b><\/b><br \/>\nuse Net::FTP;<br \/>\n#use File::Listing qw(parse_dir);<br \/>\n<b><\/b><br \/>\n# Create a new connection to the ftp server<br \/>\nsub connection {    # Create a new NET::FTP object<br \/>\n        $ftp = Net::FTP->new($CPANhost, Timeout => 60)<br \/>\n               or die \"Can't contact $CPANhost: $!\";<br \/>\n        # We shall login to the ftp server as anonymous;<br \/>\n        $ftp->login($CPANusername,$CPANpassword)<br \/>\n               or die \"Can't login ($CPANhost):\" .<br \/>\n               $ftp->message;<br \/>\n        # Change the working directory<br \/>\n        $ftp->cwd($CPANpath) or die<br \/>\n               \"Can't change directory ($CPANhost):\".<br \/>\n               $ftp->message;<br \/>\n        return $ftp;<br \/>\n    }<br \/>\n<b><\/b><br \/>\n<b><\/b><br \/>\n# The path to the CPAN\/modules directory on most CPAN hosts<br \/>\n$CPANpath = '\/';<br \/>\n<b><\/b><br \/>\n$CPANusername = 'mkuulma@ozemail.com.au';<br \/>\n$CPANpassword = '';<br \/>\n<b><\/b><br \/>\n# Change this to the name of your nearest CPAN host<br \/>\n$CPANhost = '';<br \/>\nmy $size = @ARGV;<br \/>\nif ($size == 2) {<br \/>\n  $CPANhost = 'ftp.ozemail.com.au';<br \/>\n  $CPANpath = $ARGV[1];<br \/>\n  $CPANpassword = $ARGV[0];<br \/>\n} else {<br \/>\n if ($size == 1) {<br \/>\n  $CPANhost = 'ftp.ozemail.com.au';<br \/>\n  $CPANpassword = $ARGV[0];<br \/>\n } else {<br \/>\n  if ($size == 3) {<br \/>\n   $CPANhost = $ARGV[0];<br \/>\n   $CPANusername = $ARGV[1];<br \/>\n   $CPANpassword = $ARGV[2];<br \/>\n  } else {<br \/>\n   if ($size == 4) {<br \/>\n    $CPANhost = $ARGV[0];<br \/>\n    $CPANusername = $ARGV[1];<br \/>\n    $CPANpassword = $ARGV[2];<br \/>\n    $CPANpath = $ARGV[3];<br \/>\n   } else {<br \/>\n    print \"Usage is via ... perl ftp_url.pl [ftphost=ftp.ozemail.com.au] [ftpusername=mkuulma@ozemail.com.au] ftppassword [subpath=\/]\\n\";<br \/>\n   }<br \/>\n  }<br \/>\n }<br \/>\n}<br \/>\n<b><\/b><br \/>\nif ($CPANpassword eq '') {<br \/>\n $CPANpassword = '';<br \/>\n} else {<br \/>\n # Create the initial connection<br \/>\n $ftp = connection();<br \/>\n<b><\/b><br \/>\n $ftp->pasv(); # passive mode<br \/>\n<b><\/b><br \/>\n # Retrieve a recursive directory listing<br \/>\n my @ls = $ftp->ls('-l');<br \/>\n<b><\/b><br \/>\n for my $filex (@ls) {<br \/>\n  my $file = $filex;<br \/>\n  my (@description, $size);<br \/>\n  if (-e $file) {<br \/>\n   push @description, 'binary' if (-B _);<br \/>\n   push @description, 'a socket' if (-S _);<br \/>\n   push @description, 'a text file' if (-T _);<br \/>\n   push @description, 'a block special file' if (-b _);<br \/>\n   push @description, 'a character special file' if (-c _);<br \/>\n   push @description, 'a directory' if (-d _);<br \/>\n   push @description, 'executable' if (-x _);<br \/>\n   push @description, (($size = -s _)) ? \"$size bytes\" : 'empty';<br \/>\n   push @description, (($daysago = -M _)) ? \" modified $daysago days ago\" : '';<br \/>\n   print \"\\n\\t$file is \", join(', ',@description),\"\";<br \/>\n  }<br \/>\n }<br \/>\n print \"\\n\\t\";<br \/>\n}<br \/>\n<\/code><\/p>\n<p> &#8230; or you can download it as <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.pl_GETME\">ftp_url.pl<\/a> (supervised by <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php-GETME\">ftp_url.php<\/a> changed in <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php-GETME\">this way<\/a>, where you may notice multiple HTML input type=submit buttons used to differentiate the two strands of ftp functionality modes).<\/p>\n<p>With this Perl coding we again use command line arguments as the technique to handle user defined data to define the ftp connection.<\/p>\n<p>Think server side languages, think &#8220;The Three Ps&#8221;!<\/p>\n<hr>\n<p id='ppt'>Previous relevant <a target=_blank title='Python ftp Primer Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/python-ftp-primer-tutorial\/'>Python ftp Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Python ftp Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.jpg\" title=\"Python ftp Primer Tutorial\"   \/><\/a><p class=\"wp-caption-text\">Python ftp Primer Tutorial<\/p><\/div>\n<p>Python is a great server side language to learn.  Some web server configurations use it as their &#8220;go to&#8221; server language, though ours here at rjmprogramming.com.au uses PHP (and so we&#8217;ve written a PHP &#8220;supervisor&#8221; to dovetail with today&#8217;s Python code).<\/p>\n<p>We&#8217;re going to write this Python code, that facilitates the listing of an ftp URL &#8220;path&#8221;, to run as if from a Linux (or Mac OS X, which we tested with for the work today) command line.<\/p>\n<p>So, underlying the PHP supervisory <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php\" title=\"Click picture\">live run<\/a> today, the hard working &#8220;duck child&#8221; Python goes like &#8230;<\/p>\n<p><code><br \/>\n\"\"\"<br \/>\nftp_url.py - RJM Programming - November 2017<br \/>\nShow some ftp functionality ... thanks to Python & XML by O'Reilly ISBN: 0-596-00128-2<br \/>\n\"\"\"<br \/>\nimport sys<br \/>\n<b><\/b><br \/>\nfrom urllib import urlopen<br \/>\n<b><\/b><br \/>\ndef main(argv):<br \/>\n<b><\/b><br \/>\n if len(sys.argv) == 5:<br \/>\n  fd=urlopen(\"ftp:\/\/\" + sys.argv[2] + \":\" + sys.argv[3] + \"@\" + sys.argv[1] + sys.argv[4])<br \/>\n else:<br \/>\n  if len(sys.argv) == 4:<br \/>\n   fd=urlopen(\"ftp:\/\/\" + sys.argv[2] + \":\" + sys.argv[3] + \"@\" + sys.argv[1])<br \/>\n  else:<br \/>\n   if len(sys.argv) == 3:<br \/>\n    fd=urlopen(\"ftp:\/\/mkuulma@ozemail.com.au:\" + sys.argv[1] + \"@ftp.ozemail.com.au\" + sys.argv[2])<br \/>\n   else:<br \/>\n    if len(sys.argv) == 2:<br \/>\n     fd=urlopen(\"ftp:\/\/mkuulma@ozemail.com.au:\" + sys.argv[1] + \"@ftp.ozemail.com.au\")<br \/>\n    else:<br \/>\n     print \"Usage is via ... python ftp_url.py [ftphost=ftp.ozemail.com.au] [ftpusername=mkuulma@ozemail.com.au] ftppassword [subpath=\/]\"<br \/>\n     exit()<br \/>\n<b><\/b><br \/>\n print fd.read()<br \/>\n exit()<br \/>\n<b><\/b><br \/>\nif __name__ == \"__main__\":<br \/>\n   main(sys.argv[1:])<br \/>\n<b><\/b><br \/>\n<\/code><\/p>\n<p> &#8230; or you can download it as <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.py_GETME\">ftp_url.py<\/a> (supervised by <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php_GETME\">ftp_url.php<\/a>).<\/p>\n<p>Interesting, huh?!<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d34479' onclick='var dv=document.getElementById(\"d34479\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/python\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d34479' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d34491' onclick='var dv=document.getElementById(\"d34491\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/perl\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d34491' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d34513' onclick='var dv=document.getElementById(\"d34513\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/download\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d34513' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Three Ps ride again today, building on yesterday&#8217;s Perl ftp and the Three Ps Tutorial and adding in the ability to download a single file at a time as a &#8220;Download&#8221; scenario &#8230; in modern parlance &#8230; though in &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/python-and-perl-ftp-download-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,37],"tags":[174,234,252,360,405,418,1941,469,1533,2402,707,710,916,932,997,1002,1012,2401,2334,1319,1345,2400],"class_list":["post-34513","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","tag-button","tag-command-line","tag-connection","tag-download","tag-exec","tag-file","tag-file-transfer","tag-ftp","tag-get","tag-kermit","tag-linux","tag-list","tag-perl","tag-php","tag-programming","tag-protocol","tag-python","tag-read","tag-submit","tag-tutorial","tag-url","tag-write"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/34513"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=34513"}],"version-history":[{"count":5,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/34513\/revisions"}],"predecessor-version":[{"id":34524,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/34513\/revisions\/34524"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=34513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=34513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=34513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}