The go language simply implements the FTP protocol.

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.