首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Try and Except -脚本在except上挂起

Python Try and Except -脚本在except上挂起
EN

Stack Overflow用户
提问于 2013-06-14 01:09:01
回答 3查看 1.1K关注 0票数 0

我有一个小脚本,它将检查设备列表是否启用了ssh或telnet。下面是我的代码:

代码语言:javascript
复制
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转到下一个设备。我做错了什么?谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-06-14 01:39:38

您是否尝试添加timeout

代码语言:javascript
复制
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

设置超时将在超时后关闭流,否则它将永远阻塞。

票数 1
EN

Stack Overflow用户

发布于 2013-06-14 01:17:05

我不能真正运行代码,因为我的机器上没有您打开的list文件。还是做了很少的编辑,有什么区别吗?

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2013-06-14 01:45:54

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

代码语言:javascript
复制
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

我的猜测是连接挂起了,而不是遇到了异常。可能是因为超时,因为它无法连接。

使用以下命令添加超时选项:

代码语言:javascript
复制
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(60)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17093032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档