Home  >  Article  >  Backend Development  >  Can't connect to FTP server using Go, but can connect using FileZilla

Can't connect to FTP server using Go, but can connect using FileZilla

PHPz
PHPzforward
2024-02-09 21:51:17531browse

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

php editor Zimo found that some users reported that they encountered problems when using Go language to connect to the FTP server, but they could connect normally using FileZilla. This problem may cause some developers to be unable to perform FTP operations smoothly. In this article, we will explore the possible causes and solutions to help readers solve this connection problem so that they can successfully connect to the FTP server using Go language.

Question content

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 m1 pro macbook.

Use the following command to start the container: docker run -p 22:22 -d atmoz/sftp foo:pass:::upload

go version is 1.17.13.

The program code is as follows:

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 cannot connect to the ftp server that executes this code, it produces the following output:

EOF cannot connect

I tried to connect to the same ftp server using filezilla and it worked fine and I was able to successfully connect to the server.

Any ideas on how to resolve this issue or debug the issue further? Thanks

Workaround

Port 22 is usually SSH/SFTP, not FTP. Note that FileZilla supports both FTP and SFTP. So it's likely that you're actually using FileZilla to connect with SFTP. The two protocols are completely different and incompatible.

There seems to be a "sftp" package for Go:
https://www.php.cn/link/c6344b0ae32e496be8b1b701e540d566

The above is the detailed content of Can't connect to FTP server using Go, but can connect using FileZilla. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete