导致打印失败的错误异常-python2
下面是我的代码片段。出于某种原因,它根本不会打印出第二行“破解耗时10秒”之类的内容,但第一行写着“密码找到了”:行吗。。。为什么?导致打印失败的错误异常-python2,python,exception,printing,passwords,cracking,Python,Exception,Printing,Passwords,Cracking,下面是我的代码片段。出于某种原因,它根本不会打印出第二行“破解耗时10秒”之类的内容,但第一行写着“密码找到了”:行吗。。。为什么? def connect(host, user, password, release): global Found global Fails global startTime try: s = pxssh.pxssh() s.login(host, user, password) pr
def connect(host, user, password, release):
global Found
global Fails
global startTime
try:
s = pxssh.pxssh()
s.login(host, user, password)
print '[+] Password Found: ' + password
print 'Cracking the password took' + datetime.now()-startTime + 'seconds.'
Found = True
except Exception, e:
if 'read_nonblocking' in str(e):
Fails += 1
time.sleep(5)
connect(host, user, password, False)
elif 'synchronize with original prompt' in str(e):
time.sleep(1)
connect(host, user, password, False)
问题可能是您没有设置
startTime
,但通过过度广泛的异常处理来掩盖它。删除try/except
,选择要捕获的其他异常,或者在异常处理程序中简单地包含一个裸raise
命令,您应该会看到一个名称错误,因为没有初始化。解决了这个问题,您的代码就有了更多的机会。您正在尝试连接两个不同的东西(datetime
和str
),请尝试将datetime
转换为str,如下所示:
def connect(host, user, password, release):
global Found
global Fails
global startTime
try:
s = pxssh.pxssh()
s.login(host, user, password)
print '[+] Password Found: ' + password
print 'Cracking the password took' + str(datetime.now()-startTime) + 'seconds.'
Found = True
except Exception, e:
if 'read_nonblocking' in str(e):
Fails += 1
time.sleep(5)
connect(host, user, password, False)
elif 'synchronize with original prompt' in str(e):
time.sleep(1)
connect(host, user, password, False)
此外,您不应该捕获所有类型的
异常
,只捕获您需要的异常。是否愿意共享异常?无,这是我的问题。我猜是异常导致了它出错。但我不确定。这可能是因为datetime.now()-startTime
不是字符串,请尝试使用str(datetime.now()-startTime)
。另外,Found=True
没有正确的标识Found=True确实有正确的缩进,stackoverflow只是把它搞糟了。要指出的是,我在命令行中运行此命令,因为它需要额外的参数(-t例如),如果添加str(…)
无法解决问题,请删除try except并使用回溯更新您的问题,这将有助于找到解决方案。正如上面鲁本所指出的那样,我发现了这个错误无法将“str”和“datetime.datetime”对象连接起来,在这些对象中显示了您的真实错误。必须仔细管理异常处理,以确保您不会捕获和忽略虚假错误。