zoukankan      html  css  js  c++  java
  • 远端FTP文件与本地文件如何进行Diff

    前言

    FTP 服务器是日常工作中比较常用的一种配置,可用于与外部文件共享。
    例如:作为与 A 公司合作的第三方 B 公司,A 公司每天需要获取 B 公司提供的某些关键数据信息,那么 A 公司可以通过何种方式来获取 B 公司的数据信息呢?

    答案:

    A 公司与 B 公司共同协商搭建一台 FTP 的共享服务器,设置好IP,Port,用户名,密码等关键信息,B 公司将 A 公司所需的数据信息以文件形式上传到该台 FTP 的共享服务器上,A 公司再进入到该 FTP 服务器上进行读取。

    这个过程通过 FTP 服务器完成了数据信息的共享。当然这只是 FTP 服务器比较常用的一个功能。除了使用 FTP 服务器共享数据的方式来获取数据,也可以将 A 公司与 B 公司的网络打通,通过接口调用的方式来实现数据的传输。

    工作中曾遇到过将 FTP 服务器上的文件与本地文件进行 diff 的需求,为此我使用 Python 代码实现了此需求。供大家参考。

    今日分享主题:远端 FTP 服务器上的文件与本地文件如何进行 diff。

    Python 代码实现

    1、远端服务器配置信息

    import paramiko
    import stat
    import os
     
    server_ip='ftps.test.com'
    server_port=22
    username='test'
    password='123456'

    2、从远端服务器上获取所有文件的列表

    def get_all_files_in_remote_dir(sftp,remote_dir):
         all_files=[]
         if(remote_dir[-1]=='/'):
             remote_dir=remote_dir[0:-1]
         print("-----------------",remote_dir)
         files=sftp.listdir_attr(remote_dir)
         print("=================",files)
         for file in files:
             filename=remote_dir+'/'+file.filename
            #判断文件是否是目录
            if(stat.S_ISDIR(file.st_mode)):
                all_files.extend(get_all_files_in_remote_dir(sftp,filename))
            else:
                all_files.append(filename)
        return all_files

    3、从远端服务器下载指定目录下的文件

    def sftp_get_dir(remote_dir,local_dir):
         t = paramiko.Transport((server_ip, server_port))
         t.connect(username=username, password=password)
         sftp = paramiko.SFTPClient.from_transport(t)
     
         #获取sftp上所有文件列表
         all_files=get_all_files_in_remote_dir(sftp,remote_dir)
     
         for file in all_files:
            print(file)
            dir1 = local_dir + os.path.split(file)[0]
            print("the direction is:", dir1)
            print('the mkdir command is:mkdir -p ' + dir1)
            os.popen('mkdir -p ' + dir1)
            filename = file.split('/')[-1]
            local_filename = os.path.join(dir1, filename)
            print("the file name is:", local_filename)
            if (filename.startswith('.')):
                pass
            else:
                try:
                    print("the %s file is downing" % filename)
                    sftp.get(file, local_filename)
                except:
                    print("the %s file is download fail " % filename)

    4、diff 远端服务器下载下来文件的md5值与本地文件的 md5 值

    def diff_file_md5sum(remote_dir,local_dir):
         for root,dirs,files in os.walk(remote_dir):
             for dir_list in dirs:
                 print(os.path.join(root,dir_list))
             for files_list in files:
                 remote_filename=os.path.join(root,files_list)
                 local_filename=remote_filename.replace("tony","sftp")
                 if(os.path.exists(local_filename)):
     
                    print('md5sum remote_file---'+str(remote_filename))
                    print('md5sum local_file---'+str(local_filename))
                    remote_cmd='md5sum '+str(remote_filename)
                    local_cmd='md5sum '+str(local_filename)
                    list1=os.popen(remote_cmd).readlines()
                    list2=os.popen(local_cmd).readlines()
                    try:
                        remote_md5sum=str(list1[0]).split(" ")[0]
                        local_md5sum=str(list2[0]).split(" ")[0]
                    except:
                        print("the list1 or list2 is null")
                    #对比远端文件与本地文件的md5值
                    if(remote_md5sum==local_md5sum):
                        print("the remote_md5sum is:%s, the local_md5sum is:%s" %(remote_md5sum,local_md5sum))
                        print(remote_filename," is ok")
                    else:
                        print(remote_filename,"----------------------------------------------------------------------------------------------------------------------------- of the file is error")
     
     
    if __name__ == "__main__":
        sftp_get_dir('/CoBrand/','/home/q/tony')
        sftp_get_dir('/COMM/','/home/q/tony')
        diff_file_md5sum('/home/q/tony','/home/q/sftp')

    欢迎关注【无量测试之道】公众号,回复【领取资源】
    Python编程学习资源干货、
    Python+Appium框架APP的UI自动化、
    Python+Selenium框架Web的UI自动化、
    Python+Unittest框架API自动化、
    资源和代码 免费送啦~
    文章下方有公众号二维码,可直接微信扫一扫关注即可。

    备注:我的个人公众号已正式开通,致力于测试技术的分享,包含:大数据测试、功能测试,测试开发,API接口自动化、测试运维、UI自动化测试等,微信搜索公众号:“无量测试之道”,或扫描下方二维码:


    添加关注,让我们一起共同成长!

  • 相关阅读:
    http://caibaojian.com/jquery/ JQuery在线查询手册
    验证码
    显式提交/隐式提交 //ajax方式的隐式提交
    事物 银行转账业务
    模板 Template
    登录页面跳转与错误提示信息
    连接池 八种基本类型
    文件,文件夹的基本操作--------数据流的传输
    vim编辑器
    Linux中创建和使用静态库&动态库
  • 原文地址:https://www.cnblogs.com/Wu13241454771/p/15156480.html
Copyright © 2011-2022 走看看