python尝试windows在用端口
安装 func_timeout 作用:超时强制终止函数。
import pandas as pd from func_timeout import func_set_timeout, FunctionTimedOut import socket def try_port(ip='10.129.23.203',port='8888'): try: @func_set_timeout(1) def isInuse(ip, port): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) flag=True try: s.connect((ip, int(port))) s.shutdown(1) print(port,'ok') flag=True except: print(port,'wrong1') flag=False return flag flag = isInuse(ip, port) except: print(port,'wrong2') flag=False return flag try_port(ip='127.0.0.1',port='8888') try_port(ip='10.129.23.203',port='8888') list_01 = [] for i in range(100000): flag = try_port(ip='10.129.23.203',port=i) if flag: list_01.append(i)