As I said earlier, PSCP does not support multi-threading, so we specifically implemented a
The program is divided into three parts:
1, initialization of various parameters, involving the use of getopt function
2. Define SCP, implement IP transfer and copy remotely
3, start multi-threaded call SCP, involving multi-threaded start and wait thread end
Importsys, getoptImportcommandsImportTHREADINGP= 1Host_file=""source_file=""Destin_file=""#init argumentsopts, args = Getopt.getopt (sys.argv[1:],"hp:h:")defusage ():Print "python mypscp.py [-P threadnum] [-h]-h hostfile localFile1 ... remotefullpath"#Print opts#Print argsif '- H' not inchSTR (opts)orLen (args) < 2: Usage () sys.exit () forOP, valueinchopts:ifOP = ="- P": P=Int (Str.strip (value))if(P < 1): Usage () sys.exit ()elifOP = ="- H": Usage () sys.exit ()elifOP = ="- 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 ()defSCP (IP): IP=Ip.strip () mycommand='SCP'+ Source_file +' '+ IP +':'+Destin_file#Print MyCommand(status, Output) =commands.getstatusoutput (mycommand)if(str (status) = ='0'): PrintIp'[SUCCESS]' Else: Printstatus, Output#read IP and start multi-threadIPS =[]threads= []Try: With open (Host_file) as host: forIpinchhost:ips.append (Str.strip (IP))except: PrintHost_file,'is not exist'Ipnum=Len (IPs) Threadnum=Ptimes= Ipnum/Pyushu= ipnum%P forIinchRange (Times + 1): if(Times = = 0ori = =Times ): Threadnum=Yushu forJinchRange (threadnum): IP= Ips[i*p +J]#Print IPth = Threading. Thread (TARGET=SCP, args=(IP,)) threads.append (TH) th.start () forTinchThreads:t.join ()
Test results:
PS: for reading with option parameters and Python multithreading reference the following two blog posts
Http://www.jb51.net/article/66539.htm
Http://www.cnblogs.com/fnng/p/3670789.html
PSCP multi-threaded transfer files