码迷,mamicode.com
首页 > 其他好文 > 详细

一个可以实现进度条的函数

时间:2016-06-13 19:09:57      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

 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()

 

一个可以实现进度条的函数

标签:

原文地址:http://www.cnblogs.com/along1226/p/5581539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!