英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论