线程脚本

时间:2015-10-16 14:55:36

标签: python multithreading paramiko

我编写了这个脚本,将更新文件复制到该领域的数千个客户端。除了客户端超时时,它工作正常。我正在捕获异常,但它会杀死我的脚本。希望了解如何调整此脚本以防止它死亡的一些建议。下面是异常的输出和杀死脚本的错误。

(' ABCD SAT6','超时')[Errno 113]无主机路线

追踪(最近一次通话):     文件" StackUpdateLocoImage.py",第48行,in     pool.map(worker,infile)     文件" /usr/lib64/python2.6/multiprocessing/pool.py",第148行,在地图中     return self.map_async(func,iterable,chunksize).get()     文件" /usr/lib64/python2.6/multiprocessing/pool.py" ;,第422行,获取     提高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()

1 个答案:

答案 0 :(得分:2)

您没有发现此错误:

AttributeError: 'NoneType' object has no attribute 'open_sftp_client'

问题是你有一堆除了但是你继续执行就像没有发生任何事情,当它到达这一行时:

sftp = ssh.open_sftp()

变量sshNone

因此请取消注释exit(),或者将其更改为return