python3端口扫描程序外部变量

时间:2016-01-16 22:37:32

标签: loops python-3.x while-loop

我在用户在python3的输入字段中设置ip后扫描端口范围。这项工作,但它看起来不那么专业。我有一个for循环,在那里尝试。我怎么能写出这个更优雅,没有外语课来获得一个断点?感谢。

代码如下所示:

s = socket.socket()

class outtervar():
    connected=0

while(test.outtervar==0):
    ip = getFromInputField()
    for x in range(100): 
        try:
            s.connect((ip, port + x))
            outtervar.connected=1
            break
        except socket.error as e:
            print("Cannot connect")
            print(e)
            outtervar.connected=0

1 个答案:

答案 0 :(得分:1)

您可以使用成功返回的函数:

def connect(s):
    while True:
        ip = getFromInputField()
        for x in range(100): 
            try:
                s.connect((ip, port + x))
                return
            except socket.error as e:
                print("Cannot connect")
                print(e)

s = socket.socket()                
connect(s)