欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python 3.6.7实现端口扫描器

程序员文章站 2022-05-19 12:18:25
本文实例为大家分享了python 3.6.7端口扫描器的具体代码,供大家参考,具体内容如下 环境:python 3.6.7 # -*- coding: utf-...

本文实例为大家分享了python 3.6.7端口扫描器的具体代码,供大家参考,具体内容如下

环境:python 3.6.7

# -*- coding: utf-8 -*-
import socket
import threading
import argparse

lock = threading.lock()
opennum = 0
threads = []

def portscan(ip, port):
 global opennum
 try:
 s = socket.socket(socket.af_inet, socket.sock_stream)
 s.connect((ip, port))
 lock.acquire()
 opennum = opennum + 1
 print('[+] %d open' %port)
 lock.release()
 s.close()
 
 except:
 pass
 
def main():
 parse = argparse.argumentparser()
 parse.add_argument('-h', dest = 'ip', type = str)
 args = parse.parse_args()
 ip = args.ip
 socket.setdefaulttimeout(3)
 
 for port in range(1, 1024):
 t = threading.thread(target = portscan, args = (ip, port))
 threads.append(t)
 t.start()
 
 for t in threads:
 t.join()
 
 print('[*] the host:%s scan is complete!' %(ip))
 print('[*] a total of %d open port ' %(opennum))
 
if __name__ == '__main__':
 main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。