无法使用Go连接到FTP服务器,但可以使用FileZilla连接。

huangapple go评论115阅读模式
英文:

Cannot connect to FTP server using Go but can connect using FileZilla

问题

我有一个小的Golang程序,我正在尝试连接到在Docker容器中运行的FTP服务器(https://registry.hub.docker.com/r/atmoz/sftp)。

我的机器是M1 Pro MacBook。

容器是使用以下命令启动的:
docker run -p 22:22 -d atmoz/sftp foo:pass:::upload

Go版本是1.17.13。

程序的代码如下:

package main

import (
	"log"
	"time"

	"github.com/jlaffaye/ftp"
)

func main() {
	c, err := ftp.Dial("localhost:22", ftp.DialWithTimeout(5*time.Second))
	if err != nil {
		log.Fatal(err, " cannot connect")
	}

	err = c.Login("foo", "pass")
	if err != nil {
		log.Fatal(err, "cannot login")
	}

	// Do something with the FTP conn

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

不知何故,我无法通过执行此代码连接到FTP服务器,结果输出如下:

EOF cannot connect

我尝试使用FileZilla连接到相同的FTP服务器,它可以正常工作,我能够成功连接到服务器。

对于如何修复此问题或进一步调试问题,有任何想法吗?谢谢。

英文:

I have a small Golang program and I'm trying to connect to an FTP server running in a docker container (https://registry.hub.docker.com/r/atmoz/sftp).

My machine is a M1 Pro MacBook.

The container is started with the following command:
docker run -p 22:22 -d atmoz/sftp foo:pass:::upload

The Go version is 1.17.13.

The code code of the program is the following:

package main

import (
	"log"
	"time"

	"github.com/jlaffaye/ftp"
)

func main() {
	c, err := ftp.Dial("localhost:22", ftp.DialWithTimeout(5*time.Second))
	if err != nil {
		log.Fatal(err, " cannot connect")
	}

	err = c.Login("foo", "pass")
	if err != nil {
		log.Fatal(err, "cannot login")
	}

	// Do something with the FTP conn

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

Somehow, I'm unable to connect to the FTP server executing this code, it results in the following output:

EOF cannot connect

I tried connect to the same FTP server using FileZilla and it works fine, im able to connect to the server with success.

Any ideias on how to fix this or further debug the issue? Thank you

答案1

得分: 5

端口22通常是SSH/SFTP,而不是FTP。请注意,FileZilla同时支持FTP和SFTP。所以很有可能你实际上是在使用FileZilla连接SFTP。这两个协议是完全不同且不兼容的。

Go语言似乎有一个"sftp"包:
https://pkg.go.dev/github.com/pkg/sftp

英文:

The port 22 is typically SSH/SFTP, not FTP. Note that FileZilla supports both FTP and SFTP. So chances are that you are actually connecting with SFTP using FileZilla. Those two protocols are completely different and incompatible.

There seems to be an "sftp" package for Go:
https://pkg.go.dev/github.com/pkg/sftp

huangapple
  • 本文由 发表于 2023年1月20日 19:28:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75183413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定