Sunday, April 20, 2025

Python Standard Library Examples

We will use "ftplib" module to establish connection to public remote FTP server.

Task will be to grab debian directory listing.

After we connect to host, login() function will be used to log in as "anon" user.

Then, we will relocate to "debian" directory with cwd() function.

Function retrlines() with argument "LIST" will be used to grab listing.

It's polite to close connection with close() function.


from ftplib import *

host = FTP("ftp.fi.debian.org")
host.login()
host.cwd("debian")
host.retrlines("LIST")

Result: 


-rw-r--r--    1 ftp      ftp          1185 Jul 18 11:48 README
-rw-r--r--    1 ftp      ftp          1290 Jun 26  2010 README.CD-manufacture
-rw-r--r--    1 ftp      ftp          2895 Jul 18 11:48 README.html
-rw-r--r--    1 ftp      ftp           291 Mar 04  2017 README.mirrors.html
-rw-r--r--    1 ftp      ftp            86 Mar 04  2017 README.mirrors.txt
drwxr-xr-x   19 ftp      ftp            41 Jul 18 11:50 dists
drwxr-xr-x    4 ftp      ftp            16 Jul 19 15:52 doc
-rw-r--r--    1 ftp      ftp        210880 Jul 19 16:22 extrafiles
drwxr-xr-x    3 ftp      ftp            58 Jul 19 16:17 indices
-rw-r--r--    1 ftp      ftp      12803150 Jul 19 16:17 ls-lR.gz
drwxr-xr-x    5 ftp      ftp             5 Dec 19  2000 pool
drwxr-xr-x    4 ftp      ftp             5 Nov 18  2008 project
drwxr-xr-x    3 ftp      ftp             5 Oct 10  2012 tools
drwxr-xr-x   20 ftp      ftp            20 Jul 07  2019 zzz-dists
>>> 

It's so easy to do this in Python.

Now it's time to learn how to read from file.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...