1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 ImportThreading4 Import Time5 " "6 class Demo:7 def __init__ (self,thread_num=5):8 Self.thread_num=thread_num9 def productor (self,i):Ten print "thread-%d start"%i One def start (self): A threads=[] - For x in Xrange (self.thread_num): - t=threading. Thread (target=self.productor,args= (x,)) the threads.append (t) - For T in Threads: - T.start () - For T in Threads: + T.join () - print ' All Thread end ' + A at Demo=demo () - Demo.start () - " " -thread_num=10 - defProductor (i): - Print "thread-%d Start"%I inTime.sleep (2) - defstart (): tothreads=[] + forXinchRange (thread_num): -T=threading. Thread (target=productor,args=(x,)) the threads.append (t) * forTinchThreads: $ T.start ()Panax Notoginseng forTinchThreads: - T.join () the Print 'All thread End' + A theStart ()
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 ImportParamiko4 ImportSYS5 6Private_key = Paramiko. Rsakey.from_private_key_file ('/root/.ssh/id_rsa')7 #To create an SSH object8SSH =Paramiko. Sshclient ()9 #allow connections to hosts that are not in the Know_hosts fileTen Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) Onet = Paramiko. Transport (('vm_135', 22)) A #connecting to a server -T.connect (username='Root', pkey=Private_key) -Ssh.connect (hostname='vm_135', Port=22, Username='Root', pkey=Private_key) the #Execute Command -SFTP =Paramiko. Sftpclient.from_transport (t) -stdin, stdout, stderr = Ssh.exec_command ('DF') - #Get command Results +result =Stdout.read () - Printresult + A defProgress_bar (transferred, tobetransferred, suffix="'): at #print "Transferred: {0}\tout of: {1}". Format (transferred, tobetransferred) -Bar_len = 60 -Filled_len = Int (Round (Bar_len * transferred/float (tobetransferred ))) -percents = Round (100.0 * transferred/float (tobetransferred), 1) -Bar ='='* Filled_len +'-'* (Bar_len-Filled_len) -Sys.stdout.write ('[%s]%s%s ...%s\r'% (bar, percents,'%', suffix)) in Sys.stdout.flush () -Sftp.put ("/tmp/134","/tmp/134", callback=Progress_bar) to + #For filename in filenames: - #Sftp.put (Os.path.join (dirpath, filename), the #os.path.join (remote_path, filename), * #Callback=self.progress_bar) $ #PrintPanax Notoginseng #print "Upload%s/%s"% (remote_path, filename) + ' \ t ' + ' [' + Green ' ("success") + '] ' -Ssh.close ()
A function that can implement a progress bar