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

原文地址:https://www.cnblogs.com/vincent-vg/p/6705858.html