2.1. 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¶
Assignment: FTP Download
Complexity: easy
Lines of code: 20 lines
Time: 21 min
- English:
- TODO: English Translation
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¶
Assignment: FTP Upload
Complexity: easy
Lines of code: 20 lines
Time: 21 min
- English:
- TODO: English Translation
Run doctests - all must succeed
- Polish:
Pobierz na swój komputer plik
https://python.astrotech.io/_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ść