• pscp多线程传输文件


    前面说过pscp不支持多线程,所以在此特地实现了一个

    程序分三个部分:

    1、初始化各种参数,涉及getopt函数的使用

    2、重新定义scp,实现传递IP然后远程拷贝

    3、启动多线程调用scp,涉及多线程的启动和等待线程结束

    import sys, getopt
    import commands
    import threading
    p = 1
    host_file = ""
    source_file = ""
    destin_file = ""
    
    #init arguments
    opts, args = getopt.getopt(sys.argv[1:], "Hp:h:")
    def usage():
        print "python mypscp.py [-p threadNum] [-H] -h hostFile localFile1 ... remoteFullPath"
    #print opts
    #print args
    if '-h' not in str(opts) or len(args) < 2:
        usage()
        sys.exit()
    for op, value in opts:
        if op == "-p":
            p = int(str.strip(value))
            if(p < 1):
                usage()
                    sys.exit()
        elif op == "-H":
            usage()
                sys.exit()
        elif op == "-h":
            host_file = str.strip(value)
            if(host_file == ""):
                usage()
                    sys.exit()
    source_file = str.strip(args[0])
    if(source_file == ""):
        usage()
        sys.exit()
    destin_file = str.strip(args[1])
    if(destin_file == ""):
        usage()
        sys.exit()
    
    #define scp()
    def scp(ip):
        ip = ip.strip()
        mycommand = 'scp ' + source_file + ' ' + ip + ':'+ destin_file
        #print mycommand
        (status, output) = commands.getstatusoutput(mycommand)
        if(str(status) == '0'):
            print ip,'[SUCCESS]'
        else:
            print status, output
    
    #read ip and start multi-thread
    ips = []
    threads = []
    try:
        with open(host_file) as host:
            for ip in host:
                ips.append(str.strip(ip))
    except:
        print host_file, ' is not exist'
    
    ipNum = len(ips)
    threadNum = p
    times = ipNum / p
    yushu = ipNum % p
    for i in range(times + 1):
        if(times == 0 or i == times):
            threadNum = yushu
        for j in range(threadNum):
            ip = ips[i*p + j]
            #print ip
            th = threading.Thread(target=scp, args=(ip,))
            threads.append(th)
            th.start()
        for t in threads:
            t.join()

    测试结果:

    ps:对于读取带选项参数和Python多线程参考了以下两篇博客

    http://www.jb51.net/article/66539.htm

    http://www.cnblogs.com/fnng/p/3670789.html

  • 相关阅读:
    HDU Ignatius and the Princess III (母函数)
    HDU 1014 Uniform Generator
    HDU 1013 Digital Roots
    HDU u Calculate e
    HDU 1005 Number Sequence 找规律
    Vijos 送给圣诞夜的极光(bfs)
    HDU Sum Problem (一道神坑的水题)
    Vijos CoVH之再破难关(搜索+hash)
    VIjos 晴天小猪历险记之Number (搜索+链表hash)
    Vijos 有根树的同构问题【字符串---最小表示法】
  • 原文地址:https://www.cnblogs.com/vincent-vg/p/6705858.html
Copyright © 2020-2023  润新知