0 Preface:
The FTP client believes everyone has used it, so why do we use Python to write FTP clients?
I think there are two reasons:
One is to write a better FTP client application, convenient for everyone to use;
The second is to customize some special services, such as regular daily download/upload a file;
I don't have the power or the time to write the FTP client application, first analyze the core of Python writing FTP client, that is, the key part of uploading and downloading.
First look at the FTP Server/client Architecture: 1 FTP server/client
The diagram above shows the workflow for ftp:
1. The client connects to the FTP server on the remote host.
2. The client enters a username and password (or "anonymous" and an e-mail address).
3. The client carries out various file transfer and information query operations.
4. The client exits from the remote FTP server and ends the transmission.
Here's a snippet of Python pseudocode.
From Ftplib import ftp
f = FTP (' some.ftp.server ')
f.login (' Anonymous ', ' your@email.address ')
:
F. Quit ()
Of course, this is just a general process. Sometimes, the crash of computers on either side of the network or network problems can cause the entire transmission to break before it is complete.
If the client is not responding for more than 15 minutes (900 seconds), the FTP connection times out and interrupts.
at the bottom, FTP uses TCP only, not UDP.
In addition, FTP can be viewed as a special case in client/server programming. Because the client and server here use two sockets to communicate:
One is the control and command Port (Port 21st) and the other is the data port (sometimes port 20th), as shown in the figure above.
There are two modes of FTP: active and passive. The server uses the data port only in active mode.
After the server has set port 20th as the data port, it "actively" connects to the client's data port. In passive mode, the server simply tells the client the random data port number and the client must actively establish the data connection. 2 Client FTP program example
#!/usr/bin/env python # coding=utf-8 import ftplib import OS import socket import pdb HOST = ' ftp.debian.org ' Dirn = ' d Ebian/tools ' FILE = ' Loadlin.exe ' def Main (): Try:f = Ftplib.
FTP (HOST) except (Socket.error, Socket.gaierror) as E:print ' Error:cannot reach '%s '% HOST return print ' * * * * Connected to host '%s '% host Try:f.login () except Ftplib.error_perm:print ' ERR
Or:cannot login anonymously ' f.quit () return print ' * * * logged in as "anonymous" ' Try:
F.CWD (Dirn) except Ftplib.error_perm:print ' Error:cannot CD to '%s '% Dirn f.quit () print ' * * * changed to '%s ' folder '% Dirn try:f.retrbinary (' RETR%s '% FILE, open ( File, ' WB ']. Write) except Ftplib.error_perm:print ' error:cannot read file '%s '% file Os.unlink (file ) Else:print ' * * * Download '%s ' to CWD '%file f.quit ()
if __name__ = = ' __main__ ': Main ()
Run Result:
3 Questions
The client code written above on the ftp.debian.org site can be normal connection, why some FTP server can ping but can't connect.
However, you cannot connect to the server:
Please expert advice. Thank you..