Python's network programming [1] FTP---FTP basic theory

Source: Internet
Author: User
Tags ftp client file transfer protocol ftp protocol

FTP protocol /FTP Protocol

FTP is all called file Transfer Protocol (Files Transfer Protocol), commonly used in the Internet to control the two-way transmission of files, commonly used operations are uploaded and downloaded. Based on TCP/IP, based on RFC959 communication protocol.

0 FTP theory /FTP theory

The detailed theory can be consulted here.

1 FTP structure /FTP Structure

FTP is like many other communication protocols, and the FTP protocol also uses the client / server (client/server) architecture. The user can connect to the FTP server through various FTP client programs, with the help of FTP protocol, to upload or download files.

2 FTP Communication Port /FTP Communication Port

FTP server-to-client file transfer, which needs to be done via port, the port of the FTP protocol includes:

TCP Port £ º

It is used primarily for instruction transfer, where all the client's commands to the FTP server and the server's feedback instructions are transmitted through that port.

TCP Port :

Mainly used for data transmission (active mode), such as the client uploads the download content, and view directory display content.

3 FTP Connection Mode /FTP Communication Mode

The FTP active mode and the passive mode, the difference between the two, is represented by a picture:

3.1 Active Mode PORT

Active Mode working process:

1. The client initiates a connection to server-side 21 port with a random unprivileged high-port N (>1024), and all subsequent control commands are routed through this link;

2. When data transfer is required, the client starts listening to the n+1 port and sends the Port N+1 command to the FTP server;

3. The server will actively connect The port to the client's n+1 port.

The summary process is as follows:

The client sends a connection request to the server's FTP port (by default, 21), and the server accepts the connection and establishes a command link. When the data needs to be transferred, the client tells the server with the Port command on the command link: "I opened the ***x port and you came over to connect me." The server then sends a connection request from Port 20 to the client's ***x port, creating a data link to transmit the data

Advantages of Active Mode:

Server-side configuration is simple, good for server security management, servers only need to open 21 port

Disadvantages of Active mode:

If the client has a firewall turned on, or the client is in the intranet (after the Nat Gateway), the server's connection to the client port may fail

3.2 Passive Mode PASV

Passive mode working process:

1. The client opens a random non-privileged port N and n+1, and Port N connects to port 21 of the server (submit PASV command);

2. The server opens a non-privileged port m to the passive port and returns it to the client;

3. The client actively connects the passive port m on the server side with the n+1 port to establish the connection transfer data.

The summary process is as follows:

The client sends a connection request to the server's FTP port (by default, 21), and the server accepts the connection and establishes a command link. When the data needs to be transferred, the server tells the client on the command link with the PASV command: "I opened the ***x port and you came over to connect me." The client then sends a connection request to the server's ***x port and establishes a data link to transmit the data.

Passive Mode disadvantages:

Server configuration management is slightly more complex, not conducive to security, the server needs to open a random high port so that clients can connect, so most FTP service software can manually configure the scope of the passive port

Advantages of Passive mode:

There is no requirement for the client network environment, but for the service side.

Passive Mode client Log :

*resp*'Welcome to my FTP server.'*cmd*'USER Admin'*resp*'331 Username OK, send password.'*cmd*'PASS ******'*resp*'$ Admin Login Successful'*welcome*'Welcome to my FTP server.'*cmd*'TYPE I'*resp*'The Type set to:binary.'*cmd*'PASV'*resp*'227 Entering Passive Mode (127,0,0,10,8,74).'*cmd*'RETR testfile.py'*resp*'Data Connection already open. Transfer starting.'*resp*'226 Transfer complete.'*cmd*'TYPE I'*resp*'The Type set to:binary.'*cmd*'PASV'*resp*'227 Entering Passive Mode (127,0,0,10,8,105).'*cmd*'RETR Testfile.docx'*resp*'Data Connection already open. Transfer starting.'*resp*'226 Transfer complete.'*cmd*'QUIT'*resp*'221 Goodbye, admin.'
View Code

Note:

    1. *resp* for Serveràclient, *cmd* for Clientàserver
    2. TYPE I is a binary mode transfer
    3. ' 227 Entering Passive mode (127,0,0,10,8,74). ' Indicates that the server IP and server-enabled ephemeral ports are returned, and the ephemeral port algorithm is 8*256+74=2122.

Reference Links

http://blog.csdn.net/sever2012/article/details/7074426

http://blog.csdn.net/cuker919/article/details/6403925

Python's network programming [1] FTP---FTP basic theory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.