Introduction to FTP
FTP
It is File Transfer Protocol
the abbreviation of File Transfer Protocol. FTP
is TCP
a service based only on and is not supported UDP
. It uses two ports, a data port and a command port (also called a control port). Generally speaking, these two ports are 21-命令端口
and 20-数据端口
.
FTP
There are two main modes: active mode ( PORT
)/passive mode ( PASV
), and two modes EPRT
/ EPSV
( Extended Port/Pasv
) that extend FTP again for IPv6.
Currently, most FTP
clients connect to FTP
the server in passive mode by default. The active/passive here refers to FTP
the server, whether the server opens the port and 被动
waits for the client to connect, or the server 主动
connects to the port opened by the client.
FileZilla tool
FTP
There are many commonly used tools, here is a brief introduction FileZilla
. FileZilla Chinese website , we will use it to build a service locally FTP
for testing.
Server
windows
Download address: windows FileZilla_Server_1.3.0_win64-setup.exe
After downloading, install it. Now create a user test
with the username and password 1234546
and configure the mapping folder to prepare for subsequent testing.
client
windows
Green version download address: windows FileZilla_3.58.0_win32.zip
This client can be used to verify that our service is functioning correctly.
go FTP
client
GitHub
There are many libraries available on the Internet. Here we mainly introduce FTP
the use of the client library: github.com/jlaffaye/ftp
Example
Basic example
import (
"bytes"
"commonTest/utils"
"fmt"
"github.com/jlaffaye/ftp"
)
func t1() {
c, err := ftp.Dial("127.0.0.1:21")
utils.CheckErr(err)
defer c.Quit()
// 可以重复连接,创建不同的实例
//c1, err := ftp.Dial("127.0.0.1:21")
//utils.CheckErr(err)
//defer c1.Quit()
err = c.Login("test", "123456")
utils.CheckErr(err)
// 新建文件
//reader := bytes.NewReader([]byte("12356546"))
//err = c.Stor("1.txt", reader)
//utils.CheckErr(err)
// 创建文件夹,可以嵌套 dir1/dir11
err = c.MakeDir("dir1")
utils.CheckErr(err)
fmt.Println("创建文件夹成功")
// 进入文件夹,可以嵌套 dir1/dir11
err = c.ChangeDir("dir1")
utils.CheckErr(err)
fmt.Println("进入文件夹成功")
// 新建文件
reader := bytes.NewReader([]byte("12356546"))
err = c.Stor("1.txt", reader)
utils.CheckErr(err)
fmt.Println("创建文件成功")
dir, err := c.CurrentDir()
utils.CheckErr(err)
fmt.Println("current dir: ", dir)
// 返回上一级
err = c.ChangeDirToParent()
utils.CheckErr(err)
fmt.Println(c.CurrentDir())
//entries, err := c.List("/")
//utils.CheckErr(err)
}
Make sure the folder exists
func EnsureFtpDirExist(c *ftp.ServerConn, dir string) error {
// 这里不能直接 MakeDir,有权限问题
_, err := c.List(dir)
if err != nil {
if er := c.MakeDir(dir); er != nil {
if er.Error() == "550 Directory with same name already exists." {
return nil
}
return er
}
}
return nil
}
Summarize
FTP
Mainly used for file transfer, it only supportsTCP
but notUDP
the protocol.- Divided into active/passive mode, active/passive is relative to the server.
- FileZilla is a relatively mature
FTP
product and is recommended to be used. - github.com/jlaffaye/ftp : The
go
implementedFTP
client currently only supports passive mode connection to the server. Someone mentioned thatissue
they hope to support active mode, and the author replied:
At present, basically all
FTP
services support passive mode connections. We have not found any use cases for active mode and have no plans to add it for the time being.
In addition, I would like to mention that the above test codes are all in the files in the directory of my Gitee/GoTest warehouse . This project is mainly about some records and tests of daily learning. If you are interested, you can take a look.ftp