天天看點

socket子產品判斷ip加端口的連通性

1 def check_connect(host, port):
 2     sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 3     sk.settimeout(2)
 4     status = 0
 5     try:
 6         sk.connect((host, port))
 7         print("Server port  OK!")
 8         sk.close()
 9         status = 1
10     except Exception as e:
11         error = str(e)
12         print("Server port not OK!")
13         sk.close()
14     return status