This is a creation in Article, where the information may have evolved or changed.
Package Ftpimport ("Errors" "FMT" "IO" "NET" "OS" "StrConv" "strings") type FTP struct {con net. Connip string}func newftp (IP string) (*ftp, error) {buf: = make ([]byte, 1024x768) con, err: = Net. Dial ("tcp", IP) if err! = Nil {return nil, err}n, err: = Con. Read (BUF) if err! = Nil {return nil, err}fmt. Println (String (buf[:n))) return &ftp{con, IP}, Nil}func (self *ftp) Login (user, pass string) error {buf: = make ([]byte, 1024x768) Self.con.Write ([]byte (FMT. Sprintf ("User%s\r\n", user)) Self.con.Read (BUF) Self.con.Write ([]byte (FMT. Sprintf ("Pass%s\r\n", pass)) n, Err: = Self.con.Read (BUF) if err! = Nil {return err}if!strings. Contains (String (buf[:n]), "logged on") {return errors. New (Strings. Trimspace (String (BUF)))}return Nil}func (self *ftp) PUTPASV (Pathname string) error {con, err: = Self.connection ("STOR", P Athname) If err! = Nil {return err}file, err: = OS. Open (Pathname) if err! = nil {con. Close () return Err}io. Copy (Con, File) file.close () con. Close () Buf: = Make ([]byte, 1024x768) _, Err = Self.con.Read (buf) if ERR! = Nil {return Err}return nil}func (self *ftp) GetFile (Pathname string) error {con, err: = Self.connection ("RETR", Path Name) If Err! = Nil {return err}file, err: = OS. Create (Pathname) if err! = nil {con. Close () return Err}io. Copy (File, con) file.close () con. Close () Buf: = Make ([]byte, 1024x768) _, Err = Self.con.Read (BUF) if err! = Nil {return Err}return nil}func (self *ftp) connectio N (status, Pathname string) (net. Conn, error) {buf: = make ([]byte, 1024x768) self.con.Write ([]byte ("PASV \ r \ n")) n, Err: = Self.con.Read (BUF) if err! = Nil {retur n Nil, err}if s: = string (Buf[:n]);!strings. Contains (S, "227 Entering Passive Mode") {return nil, errors. New (s)}port: = Getport (buf[27:n-3]) con, err: = Net. Dial ("TCP", FMT. Sprintf ("%s:%d", strings. Split (Self.ip, ":") [0], port)) if err! = Nil {return nil, err}self.con.write ([]byte (FMT). Sprintf ("%s%s\r\n", status, Pathname)) n, err = Self.con.Read (BUF) if err! = nil {con. Close () return nil, err}if!strings. Contains (String (buf[:n]), "Opening data channel") {con. CLose () return nil, errors. New ("Create Data Link error.")} Return con, Nil}func getport (by []byte) int {s: = string (by) List: = Strings. Split (S, ",") n1, err: = StrConv. Atoi (List[len (list)-2]) if err! = Nil {return 0}n2, err: = StrConv. Atoi (List[len (list)-1]) if err! = Nil {return 0}return n1*256 + n2}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.