1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 import threading 4 import time 5 ‘‘‘ 6 class Demo: 7 def __init__(self,thread_num=5): 8 self.thread_num=thread_num 9 def productor(self,i): 10 print "thread-%d start" %i 11 def start(self): 12 threads=[] 13 for x in xrange(self.thread_num): 14 t=threading.Thread(target=self.productor,args=(x,)) 15 threads.append(t) 16 for t in threads: 17 t.start() 18 for t in threads: 19 t.join() 20 print ‘all thread end‘ 21 22 23 demo=Demo() 24 demo.start() 25 ‘‘‘ 26 thread_num=10 27 def productor(i): 28 print "thread-%d start" %i 29 time.sleep(2) 30 def start(): 31 threads=[] 32 for x in range(thread_num): 33 t=threading.Thread(target=productor,args=(x,)) 34 threads.append(t) 35 for t in threads: 36 t.start() 37 for t in threads: 38 t.join() 39 print ‘all thread end‘ 40 41 42 start()
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 import paramiko 4 import sys 5 6 private_key = paramiko.RSAKey.from_private_key_file(‘/root/.ssh/id_rsa‘) 7 # 创建SSH对象 8 ssh = paramiko.SSHClient() 9 # 允许连接不在know_hosts文件中的主机 10 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 11 t = paramiko.Transport((‘vm_135‘,22)) 12 # 连接服务器 13 t.connect(username=‘root‘,pkey=private_key) 14 ssh.connect(hostname=‘vm_135‘, port=22, username=‘root‘,pkey=private_key) 15 # 执行命令 16 sftp = paramiko.SFTPClient.from_transport(t) 17 stdin, stdout, stderr = ssh.exec_command(‘df‘) 18 # 获取命令结果 19 result = stdout.read() 20 print result 21 22 def progress_bar(transferred, toBeTransferred, suffix=‘‘): 23 # print "Transferred: {0}\tOut of: {1}".format(transferred, toBeTransferred) 24 bar_len = 60 25 filled_len = int(round(bar_len * transferred/float(toBeTransferred))) 26 percents = round(100.0 * transferred/float(toBeTransferred), 1) 27 bar = ‘=‘ * filled_len + ‘-‘ * (bar_len - filled_len) 28 sys.stdout.write(‘[%s] %s%s ...%s\r‘ % (bar, percents, ‘%‘, suffix)) 29 sys.stdout.flush() 30 sftp.put("/tmp/134","/tmp/134",callback=progress_bar) 31 32 #for filename in filenames: 33 # sftp.put(os.path.join(dirpath, filename), 34 # os.path.join(remote_path, filename), 35 # callback=self.progress_bar) 36 # print 37 # print "upload %s/%s" % (remote_path, filename) + ‘\t‘ + ‘[‘ + green("success") + ‘]‘ 38 ssh.close()
时间: 2024-10-03 22:49:23