Python socket error WinError 1002 provided an invalid parameter

< H1 > problem description < / H1 >

beginner python, wants to be a TCP port open batch inspection tool.

< H1 > Code < / H1 >
import socket

port=int("13389")

socket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.settimeout(5)

f=open("ip.txt","r",encoding="UTF-8")
lines=f.readlines()
for ip in lines:
    host=ip.strip("\n")
    try:
        socket.connect((host,port))
        result=(" TCP:{}:{} !").format(host,port)
        print(result)
    except Exception as error:
        result=(" TCP:{}:{} ! :{}").format(host,port,error)
        print(result)
< H1 > run results < / H1 >
 TCP:127.0.0.1:13389 ! :[WinError 10061] 
 TCP:192.168.1.1:13389 ! :timed out
 TCP:192.168.1.2:13389 ! :[WinError 10022] 
 TCP:192.168.1.3:13389 ! :[WinError 10022] 
 TCP:192.168.1.4:13389 ! :[WinError 10022] 

you can see from the result that after the first loop completes, the second loop works normally, but as long as the timed out, is thrown, the next loop will report that [WinError 10022] provided an invalid parameter. May I ask how to solve this problem?

Mar.05,2021
Menu