我編寫了此腳本以將更新文件複製到數千個字段中的客戶端。它適用於客戶超時的情況。我正在捕獲異常,但它殺死了我的腳本。將不勝感激一些建議,如何調整此腳本,以防止死亡。以下是異常的輸出和錯誤消除腳本。線程腳本中的Python腳本open_sftp_client錯誤
( 'ABCD SAT6', '超時')[錯誤113]沒有舉辦
回溯(最近通話最後一個)路線: 文件 「StackUpdateLocoImage.py」,第48行,在 池。 map(worker,infile) 文件「/usr/lib64/python2.6/multiprocessing/pool.py」,第148行,在地圖中 return self.map_async(func,iterable,chunksize).get() File「/ usr/lib64/python2.6/multiprocessing/pool.py「,第422行,獲得 raise self._value AttributeError:'NoneType'對象沒有屬性'open_sftp_client'
#!/usr/bin/python
import paramiko, os, string, threading
import socket
import sys
import logging
import multiprocessing
##Python Logging##
LOG_FILENAME = 'updateLocos.out'
logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG,)
## Paramiko Logging##
paramiko.util.log_to_file("Paramiko_Connections.log")
def worker(line):
locoid = line.split()
try:
if locoid[0] == 'ABCD' :
locoip = locoid[4]
##Start SFTP
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(locoip, username='user', password='password', timeout=3.0)
except paramiko.AuthenticationException,e:
print ('ABCD ' + locoid[1], 'Auth Error: ',e)
#exit()
except paramiko.SSHException,e:
print "Connection Error: ",e
#exit()
except socket.error,e:
print ('ABCD ' + locoid[1], 'Timed Out'),e
#exit()
filepath = '/usr/local/bin/scripts/update3.2.2.tar.gz'
localpath = '/root/update3.2.2.tar.gz'
sftp = ssh.open_sftp()
sftp.put(filepath, localpath)
sftp.close()
ssh.close()
## set up processing pool
pool = multiprocessing.Pool()
with open('hostslists.txt') as infile:
pool.map(worker, infile)
pool.close()
pool.join()
是的,將它改爲return()修復了這個問題。謝謝。 – teamg