Python ftp Primer Tutorial

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.

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>