为什么我没有抓住套接字超时?

时间:2018-04-19 13:27:52

标签: python sockets timeout

我不明白,为什么在下面的代码中我没有超时(即使我用settimeout设置它,我也看不到捕获的异常):

import socket

HOST = 'x.x.x.x'

open_ports = []
closed_ports = []

def scan(PORT):
    sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #sock_tcp.settimeout(0.5)
    SERVER_ADDR = (HOST, PORT)
    try:
        portStatusTCP = sock_tcp.connect_ex(SERVER_ADDR)
        if portStatusTCP==0:
            open_ports.append(PORT)
            print(str(PORT)+' is OPEN')
        else:
            closed_ports.append(PORT)
            print(str(PORT)+' is CLOSED')
    except socket.error as socketerror:
        print("ERROR"+socketerror)
    sock_tcp.close()

for PORT in range(15,25):
    print('scanning port: '+str(PORT))
    scan(PORT)

print(open_ports)
print(closed_ports)

0 个答案:

没有答案