Python Try and Except - 脚本挂在except上

时间:2013-06-13 17:09:01

标签: python

我有一个小脚本,将检查设备列表是ssh还是telnet启用。 这是我的代码:

import socket
import sys
file = open('list', 'r')
file = file.readlines()

list = []

for i in file:
    i=i.replace('\n','')
    list.append(i)
for i in list:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.connect((i, 22))
        s.shutdown(2)
        s.close()
        print (i+' SSH ')
    except:
        try:
            s.connect((i, 23))
            s.shutdown(2)
            s.close()
            print (i+' Telnet')
        except:
            print (i + 'disable')
            pass

当我得到异常时,我必须按ctrl + c转到下一个设备。我做错了什么?感谢

3 个答案:

答案 0 :(得分:1)

我无法真正运行代码,因为我没有在您的计算机上打开list文件。还是做了一些编辑,有什么不同吗?

import socket
import sys
file = open('list', 'r')
file = file.readlines()

list = []

for i in file:
    i=i.replace('\n','')
    list.append(i)
for i in list:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
         s.connect((i, 22))
         s.shutdown(2)
         s.close()
         print (i+' SSH ')
    except:
         s.connect((i, 23))
         s.shutdown(2)
         s.close()
         print (i+' Telnet')
    else:
         print (i + 'disable')
         pass

答案 1 :(得分:1)

您是否尝试添加timeout

import socket
import sys
with open('list', 'r') as f:# file is a global class

    # per default it reads the file line by line, 
    # readlines() loads the whole file in memory at once, using more memory
    # and you don't need the list.
    for i in f:
        i=i.replace('\n','')
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(10)
        try:
            s.connect((i, 22))
            s.shutdown(2)
            s.close()
            print (i+' SSH ')
        except:
            try:
                s.connect((i, 23))
                s.shutdown(2)
                s.close()
                print (i+' Telnet')
            except:
                print (i + 'disable')
                pass

设置超时会在超时后关闭流,否则会永久阻止。

答案 2 :(得分:0)

我再次无法运行代码,因为缺少文件'list'(相当误导......)但我已经做了一些进一步的重构并提出了一个建议。

import socket
import sys

with open('list', 'r') as f:
    # Don't call anything 'list' as it is the name for pythons inbuilt type
    # Using `with` will automatically close the file after you've used it.
    content = f.readlines()
    # We can use a list comprehension to quickly strip any newline characters.
    content = [x.replace('\n','') for x in content]

for x in content:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.connect((x, 22))
        s.shutdown(2)
        s.close()
        print (x+' SSH')
    except:
    # This should catch a specific exception, e.g. TimeoutError etc
    # e.g. `except ConnectionError`
        try:
            s.connect((i, 23))
            s.shutdown(2)
            s.close()
            print (i+' Telnet')
        except:
            print (i + 'disable')
            pass

我的猜测是连接挂起而不是命中异常。可能由于超时而无法连接。

使用以下内容添加超时选项:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(60)