「爬FTP数据并下载到本地(python)」

 感觉网上很多的东西比较零零散散,根据这次的任务要求整合一个可以扫描网段的ip地址并爬取IP地址中可以匿名登陆FTP的数据(有原创有学习)

  1. 扫描一个网段,获取其中所有的开放FTP服务的机器的IP地址
  2. 依次获取每个FTP的文件目录内容,从每个FTP中下载一定量的文件
    import platform
    import sys
    import os
    import time
    import _thread
    import datetime
    from ftplib import FTP
    
    
    class FTP_P(FTP):
        def dirs(self, *args):
            cmd = 'LIST'
            templist = []
            func = None
            if args[-1:] and type(args[-1]) != type(''):
                args, func = args[:-1], args[-1]
            for arg in args:
                if arg:
                    cmd = cmd + (' ' + arg)
            self.retrlines(cmd, templist.append)
            return templist
    
    def download_single_url_ftp(stri):
        try:
            ftp = FTP_P()
            ftp.encoding = "utf-8"
            ftp.connect(stri,21)  # 连接的ftp sever和端口
            ftp.login("anonymous")  # 连接的用户名,密码
            # print(ftp.getwelcome()) #打印出欢迎信息
            ftp.cwd(r"/")  # 设置FTP当前操作的路径
            for i in ftp.dirs():
                print(i+'')
            ftpath = '/'
            localpath = 'D:/data/'
            ftpDownload(ftp,ftpath,localpath)
            ftp.quit()  # 退出ftp
        except (ConnectionRefusedError, TimeoutError, WindowsError) as e:
                print("{}的Ftp连接失败 {}".format(stri,str(e)))
                print()
                pass
        except BaseException as e:
                print("{}的Ftp连接失败 {}".format(stri,str(e)))
                print()
                pass
    
    
    def ftpDownload(ftp, ftpath, localpath):
        '''
        :param ftp: 登陆ftp返回的信息
        :param ftpath: ftp中的目标路径
        :param localpath: 存放下载文件的本地路径
        :return:
        '''
        print('Remote Path: {0}'.format(ftpath))
        if not os.path.exists(localpath):
            os.makedirs(localpath)#如果文件不在创建文件
        for file in ftp.nlst():
            print('file:', file)
            if len(ftp.nlst(file)) == 0:
                pass
            elif file == ftp.nlst(file)[0]:
                print('扫描到文件')
                ftpDownloadFile(ftp,file, localpath)
            else:
                print('扫描到文件夹')
                path = ftp.pwd() + '/' + file
                local = localpath+'/'+ file
                ftp.cwd(path)
                ftpDownload(ftp, path, local)#递归
                ftp.cwd('..')
        return True
    #python saomiao.py 149
    
    def ftpDownloadFile(ftp, ftpfile, localfile):
        bufsize = 1024
        path = os.path.join(localfile,ftpfile)
        with open(path, 'wb') as fid:
            print('正在下载:',ftpfile)
            ftp.retrbinary('RETR {0}'.format(ftpfile), fid.write, bufsize)  # 接收服务器文件并写入本地文件
            print('下载完毕。')
        return True
    
    
    def get_os():
        os = platform.system()
        if os == "Windows":
            return "n"
        else:
            return "c"
    
    def ping_ip(ip_str):
        cmd = ["ping", "-{op}".format(op=get_os()),
               "1", ip_str]
        output = os.popen(" ".join(cmd)).readlines()
    
        flag = False
        for line in list(output):
            if not line:
                continue
            if str(line).upper().find("TTL") >=0:#判断存活时间
                flag = True
                break
        if flag:
            print("*** *** *** ip: %s 可以ping通  *** *** ***"%(ip_str))
            download_single_url_ftp(ip_str)
    
    def find_ip(ip_prefix):
        for i in range(1,256):
            ip = ('%s.%s'%(ip_prefix,i))
            _thread.start_new_thread(ping_ip, (ip,))
            time.sleep(0.5)
            # ping_ip(ip)
    
    if __name__ == "__main__":
        startTime = datetime.datetime.now()
        print("start time %s"%(time.ctime()))
        net=sys.argv[1]
        args = "".join(("211.71."+net+".1"))#211.71.149
        ip_prefix = '.'.join(args.split('.')[:-1])#211.71.149
    
        find_ip(ip_prefix)
    
        endTime = datetime.datetime.now()
        print("end time %s"%(time.ctime()))
        print("total takes :",(endTime - startTime).seconds)
    
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值