2.1. Protocol FTP
2.1.1. Connect to FTP Server (insecure)
from ftplib import FTP
FTP_HOST = 'ftp.pureftpd.org'
FTP_USER = 'anonymous'
FTP_PASS = ''
with FTP(host=FTP_HOST, user=FTP_USER, passwd=FTP_PASS, timeout=30) as ftp:
ftp.dir()
# drwxr-xr-x 18 1000 1008 1024 Jul 21 2016 .
# drwxr-xr-x 18 1000 1008 1024 Jul 21 2016 ..
# lrwxr-xr-x 1 1000 20 20 Jun 20 2011 6jack -> pure-ftpd/misc/6jack
# lrwxr-xr-x 1 1000 1008 12 Feb 24 2012 OpenBSD -> misc/OpenBSD
# drwxr-xr-x 2 1000 1008 512 Feb 10 2015 antilink
# lrwxr-xr-x 1 0 1008 24 Feb 1 2006 blogbench -> pure-ftpd/misc/blogbench
# lrwxr-xr-x 1 0 1008 21 Feb 1 2006 bsdcam -> pure-ftpd/misc/bsdcam
# ...
2.1.2. Connect to FTP Server (secure)
from ftplib import FTP_TLS
FTP_HOST = 'ftp.us.debian.org'
FTP_USER = 'anonymous'
FTP_PASS = ''
with FTP_TLS(host=FTP_HOST, user=FTP_USER, passwd=FTP_PASS, timeout=30) as ftps:
ftps.dir()
# drwxr-xr-x 18 1000 1008 1024 Jul 21 2016 .
# drwxr-xr-x 18 1000 1008 1024 Jul 21 2016 ..
# lrwxr-xr-x 1 1000 20 20 Jun 20 2011 6jack -> pure-ftpd/misc/6jack
# lrwxr-xr-x 1 1000 1008 12 Feb 24 2012 OpenBSD -> misc/OpenBSD
# drwxr-xr-x 2 1000 1008 512 Feb 10 2015 antilink
# lrwxr-xr-x 1 0 1008 24 Feb 1 2006 blogbench -> pure-ftpd/misc/blogbench
# lrwxr-xr-x 1 0 1008 21 Feb 1 2006 bsdcam -> pure-ftpd/misc/bsdcam
# ...
2.1.3. Download file
from ftplib import FTP
FTP_HOST = 'ftp.us.debian.org'
FTP_USER = 'anonymous'
FTP_PASS = ''
with FTP(host=FTP_HOST, user=FTP_USER, passwd=FTP_PASS, timeout=30) as ftp:
ftp.cwd('debian') # change into "debian" directory
ftp.dir() # list directory contents
# drwxr-xr-x 9 ftp ftp 4096 Nov 06 21:00 debian
# drwxr-xr-x 9 ftp ftp 4096 Nov 06 13:57 debian-archive
# drwxr-sr-x 5 ftp ftp 4096 Mar 13 2016 debian-backports
# drwxr-xr-x 2 ftp ftp 4096 Jun 22 2015 stats
with open('README', mode='wb') as file:
ftp.retrbinary('RETR README', file.write)
# '226 Transfer complete.'
2.1.4. Methods
Method |
Arguments |
Description |
---|---|---|
|
|
Connect to the given host and port. The default port number is 21, as specified by the FTP protocol specification |
|
|
Log in as the given user |
|
Abort a file transfer that is in progress |
|
|
|
Send a simple command string to the server and return the response string |
|
|
Retrieve a file in binary transfer mode |
|
|
Retrieve a file or directory listing in ASCII transfer mode |
|
|
Enable 'passive' mode if val is true, otherwise disable passive mode |
|
|
Store a file in binary transfer mode |
|
|
Store a file in ASCII transfer mode |
|
|
Produce a directory listing as returned by the LIST command, printing it to standard output |
|
|
Rename file |
|
|
Remove the file |
|
|
Set the current directory on the server |
|
|
Create a new directory on the server |
|
Return the pathname of the current directory on the server |
|
|
|
Remove the directory named dirname on the server |
|
|
Request the size of the file named filename on the server |
2.1.5. Assignments
2.1.5.1. FTP Download
- About:
Name: FTP Download
Difficulty: easy
Lines: 20
Minutes: 21
- License:
Copyright 2025, Matt Harasymczuk <matt@python3.info>
This code can be used only for learning by humans (self-education)
This code cannot be used for teaching others (trainings, bootcamps, etc.)
This code cannot be used for teaching LLMs and AI algorithms
This code cannot be used in commercial or proprietary products
This code cannot be distributed in any form
This code cannot be changed in any form outside of training course
This code cannot have its license changed
If you use this code in your product, you must open-source it under GPLv2
Exception can be granted only by the author (Matt Harasymczuk)
- English:
Create on your computer file named
firstname-lastname.txt
, where:instead of
firstname
put your first nameinstead of
lastname
put your last name
To the file paste text from PEP 20 -- The Zen of Python (result of
import this
command)Connect to FTP Server provided by lecturer
Download file
README.txt
from main folderUpload file
firstname-lastname.txt
tofiles
folderUse Context Manager to connect
Run doctests - all must succeed
- Polish:
Stwórz na swoim komputerze plik o nazwie
imie-nazwisko.txt
, gdzie:zamiast
imie
wpisz swoje imięzamiast
nazwisko
wpisz swoje nazwisko
Do pliku wklej treść tekstu PEP 20 -- The Zen of Python (wynik polecenia
import this
)Połącz się z serwerem podanym przez prowadzącego
Pobierz plik
README.txt
z głównego folderuDo katalogu
files
uploaduj plikimie-nazwisko.txt
Skorzystaj z Context Managera do połączenia
Uruchom doctesty - wszystkie muszą się powieść
2.1.5.2. FTP Upload
- About:
Name: FTP Upload
Difficulty: easy
Lines: 20
Minutes: 21
- License:
Copyright 2025, Matt Harasymczuk <matt@python3.info>
This code can be used only for learning by humans (self-education)
This code cannot be used for teaching others (trainings, bootcamps, etc.)
This code cannot be used for teaching LLMs and AI algorithms
This code cannot be used in commercial or proprietary products
This code cannot be distributed in any form
This code cannot be changed in any form outside of training course
This code cannot have its license changed
If you use this code in your product, you must open-source it under GPLv2
Exception can be granted only by the author (Matt Harasymczuk)
- English:
Download file
https://python3.info/_static/favicon.png
to your computerCreate on your computer file named
firstname-lastname.png
, where:instead of
firstname
put your first nameinstead of
lastname
put your last name
Connect to FTP Server provided by lecturer
Upload file downloaded in previous step to
img
folderUse Context Manager to connect
Transfer data in binary mode
Run doctests - all must succeed
- Polish:
Pobierz na swój komputer plik
https://python3.info/_static/favicon.png
Nazwij plik
imie-nazwisko.png
, gdzie:zamiast
imie
wpisz swoje imięzamiast
nazwisko
wpisz swoje nazwisko
Połącz się z serwerem podanym przez prowadzącego
Do katalogu
img
uploaduj plik pobrany w poprzednim krokuSkorzystaj z Context Managera do połączenia
Przesyłanie danych ma odbywać się w trybie binarnym
Uruchom doctesty - wszystkie muszą się powieść