2.1. FTP

2.1.1. Connect to FTP Server (insecure)

Code 2.52. 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)

Code 2.53. 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

Code 2.54. Download file from FTP Server
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

Table 2.10. FTP Methods

Method

Arguments

Description

FTP.connect()

host='', port=0, timeout=None, source_address=None

Connect to the given host and port. The default port number is 21, as specified by the FTP protocol specification

FTP.login()

user='anonymous', passwd='', acct=''

Log in as the given user

FTP.abort()

Abort a file transfer that is in progress

FTP.sendcmd()

cmd

Send a simple command string to the server and return the response string

FTP.retrbinary()

cmd, callback, blocksize=8192, rest=None

Retrieve a file in binary transfer mode

FTP.retrlines()

cmd, callback=None

Retrieve a file or directory listing in ASCII transfer mode

FTP.set_pasv()

val

Enable 'passive' mode if val is true, otherwise disable passive mode

FTP.storbinary()

cmd, fp, blocksize=8192, callback=None, rest=None

Store a file in binary transfer mode

FTP.storlines()

cmd, fp, callback=None

Store a file in ASCII transfer mode

FTP.dir()

argument[, ...]

Produce a directory listing as returned by the LIST command, printing it to standard output

FTP.rename()

old_name, new_name

Rename file

FTP.delete()

filename

Remove the file

FTP.cwd()

pathname

Set the current directory on the server

FTP.mkd()

pathname

Create a new directory on the server

FTP.pwd()

Return the pathname of the current directory on the server

FTP.rmd()

dirname

Remove the directory named dirname on the server

FTP.size()

filename

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:
  1. Create on your computer file named firstname-lastname.txt, where:

    1. instead of firstname put your first name

    2. instead of lastname put your last name

  2. To the file paste text from PEP 20 -- The Zen of Python (result of import this command)

  3. Connect to FTP Server provided by lecturer

  4. Download file README.txt from main folder

  5. Upload file firstname-lastname.txt to files folder

  6. Use Context Manager to connect

  7. Run doctests - all must succeed

Polish:
  1. Stwórz na swoim komputerze plik o nazwie imie-nazwisko.txt, gdzie:

    1. zamiast imie wpisz swoje imię

    2. zamiast nazwisko wpisz swoje nazwisko

  2. Do pliku wklej treść tekstu PEP 20 -- The Zen of Python (wynik polecenia import this)

  3. Połącz się z serwerem podanym przez prowadzącego

  4. Pobierz plik README.txt z głównego folderu

  5. Do katalogu files uploaduj plik imie-nazwisko.txt

  6. Skorzystaj z Context Managera do połączenia

  7. 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:
  1. Download file https://python3.info/_static/favicon.png to your computer

  2. Create on your computer file named firstname-lastname.png, where:

    1. instead of firstname put your first name

    2. instead of lastname put your last name

  3. Connect to FTP Server provided by lecturer

  4. Upload file downloaded in previous step to img folder

  5. Use Context Manager to connect

  6. Transfer data in binary mode

  7. Run doctests - all must succeed

Polish:
  1. Pobierz na swój komputer plik https://python3.info/_static/favicon.png

  2. Nazwij plik imie-nazwisko.png, gdzie:

    1. zamiast imie wpisz swoje imię

    2. zamiast nazwisko wpisz swoje nazwisko

  3. Połącz się z serwerem podanym przez prowadzącego

  4. Do katalogu img uploaduj plik pobrany w poprzednim kroku

  5. Skorzystaj z Context Managera do połączenia

  6. Przesyłanie danych ma odbywać się w trybie binarnym

  7. Uruchom doctesty - wszystkie muszą się powieść