Python多线程实现常见端口扫描
import threading
import sys
import socket
import threading
import sys
port_list = [21, 22, 23, 25, 80, 135, 137, 139, 445, 1433, 1502, 3306, 3389, 8080 ,9015]
target_ip = sys.argv[1]
#print target_ip
Lock = threading.Lock()
def scan_port(target_ip,target_port):
#global target
try:
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((target_ip,target_port))
Lock.acquire()
print u'port :'+str(target_port)+' is opening'
Lock.release()
s.close()
except Exception,e:
#with Lock:
pass
if __name__ == '__main__':
for port in port_list:
#scan_port(target_ip,port)
t=(threading.Thread(target = scan_port,args = (target_ip,port)))
t.setDaemon(True)
t.start()
t.join()
本地测试效果如下:
python菜鸟,练手中……