jlaffaye / ftp

FTP client package for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

229 Entering Extended Passive Mode (|||45368|) when attempting to read the file repeatedly after creating a connection only once. Report an error

chenpb-lu opened this issue · comments

func t(filePath string) {
	c, err := ftp.Dial(ftpServer, ftp.DialWithTimeout(5*time.Second))
	if err != nil {
		log.Fatal(fmt.Sprintf("filePath: %s err: %v", filePath, err))
	}

	err = c.Login(ftpUser, ftpPasswd)
	if err != nil {
		log.Fatal(fmt.Sprintf("filePath: %s err: %v", filePath, err))
	}

	for i := 0; i < 5; i++ {
		_, err := c.Retr(filePath)
		if err != nil {
			log.Fatal(fmt.Sprintf("filePath: %s err: %v", filePath, err))
		}
	}
	if err := c.Quit(); err != nil {
		log.Fatal(err)
	}
}

The package version is github.com/jlaffaye/ftp v0.2.0

It's because you're not closing the file. Try this:

func t(filePath string) {
	conn, err := ftp.Dial(ftpServer, ftp.DialWithTimeout(5*time.Second))
	if err != nil {
		log.Fatal(fmt.Sprintf("filePath: %s err: %v", filePath, err))
	}

	if err = conn.Login(ftpUser, ftpPasswd); err != nil {
		log.Fatal(fmt.Sprintf("filePath: %s err: %v", filePath, err))
	}

	for i := 0; i < 5; i++ {
		// pull file
		resp, err := conn.Retr(filePath)
		if err != nil {
			log.Fatal(fmt.Sprintf("filePath: %s err: %v", filePath, err))
		}

		// do something with the file

		// close the file
		if err = resp.Close(); err != nil {
			log.Fatal(err)
		}
	}

	if err = conn.Quit(); err != nil {
		log.Fatal(err)
	}
}