PSCP multi-threaded transfer files

Source: Internet
Author: User
Tags pscp



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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.