工作自动化-ftp

一套小型工作自动化系统。

使用语言:Python3.

主要功能:FTP下载、zip解压、文件重命名、Excle中执行vba宏、Excle数据合并。

 

FTP主要方法:(介绍项目用到的方法)

connect(ip, port):连接FTP服务器。

login(user, passwd):FTP登录。

cwd(path):进入FTP服务器的某个目录。(Come to Work Directory)

retrbinary(cmd, cb):执行ftp命令。

cmd:要执行的指令。

cb:callback。eg:open(file, "wb").write

quit():FTP退出。资源释放。

 

将ftp模块化封装,使用起来会更加方便。可以在hub下载相关代码:git@gitee.com:powerbinbin/auto_sys_dialy.git

FTP非常简单,直接上代码:

 

from ftplib import FTP

debug=False

class ftp():
   def __init__(self):
       if debug is True:
           print("class-ftp init...")
       self.ftp=FTP()
       if debug is True:
           print("class-ftp init done")

   def login(self, user='binbin', passwd='Public1223', ip="192.168.0.103", port=5432, path=None):
      if ip is None:
          print("Login Err: ip shouldn't be None!")
          exit(-1)

      try:
          self.ftp.connect(ip, port)
          if debug is True:
            print("class-ftp login: connect done")
      except Exception as err:
          print(err)
          exit(-1)

      try:
          self.ftp.login(user, passwd)
          if debug is True:
            print("class-ftp login: login done")
      except Exception as err:
          print(err)
          exit(-1)

      try:
          if path is not None:
              self.ftp.cwd(path)
      except Exception as err:
          print(err)
          exit(-1)

   def download(self, file=None):
        if debug is True:
            print("class-ftp login: download...[%s]" %file)

        if file is None:
            print("download: file shouldn't be None!")
            exit(-1)

        try:
            cb=open(file, "wb").write
            cmd='RETR '+file
            self.ftp.retrbinary(cmd, callback=cb)
        except Exception as err:
            print(err)
            exit(-1)

        if debug is True:
            print("class-ftp login: download done [%s]" %file)
        return file

   def close(self):
      self.ftp.quit()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值