python怎么检测端口是否被占用?
有个需求就是检测69端口是否被占用?然后给出提示,怎么给出window的系统提示?
import socket
ports = []
for i in [139,445,80,135,137,1433,3389,8080,9015,137,21,23,25,1502]: #这里可写你要检测的常用端口列表
sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sk.settimeout(1)
try:
sk.connect(('*.*.*.*',i)) #'*.*.*.*'写上你要接入的IP地址
except Exception:
print i, len(ports)
continue
sk.close()
# sk.__init__()
ports.append(i)
print i, len(ports), '*'
i += 1
print ports
[ 本帖最后由 lampeter123 于 2009-12-26 08:32 编辑 ]