Go语言简单实现FTP协议.
来源:互联网 发布:局域网怎样破解mac上网 编辑:程序博客网 时间:2024/04/30 20:27
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, 1024)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, 1024)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]), "230 Logged on") {return errors.New(strings.TrimSpace(string(buf)))}return nil}func (self *Ftp) PutPasv(Pathname string) error {con, err := self.connection("STOR", Pathname)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, 1024)_, err = self.con.Read(buf)if err != nil {return err}return nil}func (self *Ftp) GetFile(Pathname string) error {con, err := self.connection("RETR", Pathname)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, 1024)_, err = self.con.Read(buf)if err != nil {return err}return nil}func (self *Ftp) connection(status, Pathname string) (net.Conn, error) {buf := make([]byte, 1024)self.con.Write([]byte("PASV \r\n"))n, err := self.con.Read(buf)if err != nil {return 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]), "150 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}
1 0
- Go语言简单实现FTP协议.
- go语言Socket Server 自定义协议的简单实现
- go语言实现简易ftp客户端
- Java语言实现简单FTP软件------>FTP协议分析(一)
- raft协议的go语言实现
- ftp协议c语言实现-linux平台
- Go语言实现简单的留言本
- Go语言实现简单的文件服务器
- go语言实现的简单web服务器
- Go语言 实现简单的 Set
- Go语言实现简单的web服务器
- Java语言实现简单FTP软件
- Memcached协议解析及Go语言客户端实现
- 用C语言实现FTP协议客户端的主要功能
- 用C语言实现FTP协议客户端的主要功能
- GO语言简单语法
- FTP协议Java实现
- telnet 实现ftp协议
- 2015年10个重大突破性技术
- Java - What is difference between HashMap and Hashtable?
- 3dsmax模型改名程序
- the basic operation of sequence in python
- MySql常用操作指令
- Go语言简单实现FTP协议.
- eclipse 导包
- 3dsMax 按物体分离材质
- onNewIntent
- MD5与 RSA
- 某网站提供的香港空间,美国空间,国内空间测速结果
- sql之left join、right join、inner join的区别
- 将NSString转换成UTF8编码的NSString
- Alfred工具使用<1>