python检测端口是否开放

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

python的socket库可以检测端口是否开放

import socket;
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('127.0.0.1',80))
if result == 0:
   print "Port is open"
else:
   print "Port is not open"

这里sock.connect_ex方法尝试连接如果连接成功返回0。