python 实现端口扫描工具

作者:lucky_qi 时间:2022-12-05 23:02:45 


# 简单的端口扫描工具
# 作者: Charles
# 公众号: Charles的皮卡丘
import time
import socket
import threading

# 判断是否为正确的IP地址。
def isIP(ip):
 ip_addr = ip.split('.')
 if len(ip_addr) != 4:
  return False
 for ipnum in ip_addr:
  if not (0 <= int(ipnum) < 255):
    return False
 else:
  return True

# 端口扫描工具
class scanThread(threading.Thread):
 def __init__(self, ip, port_min=0, port_max=65535):
  # 初始化。
  threading.Thread.__init__(self)
  self.port_max = port_max
  self.port_min = port_min
  self.ip = ip
  # assert isinstance(int,self.port_min) and isinstance(int,self.port_max)
 # 重写run
 def run(self):
  return self.__checker()
 # 检测
 def __checker(self):

for port in range(self.port_min,self.port_max):
    self.__connect(port)
 # 连接
 def __connect(self,port):
  socket.setdefaulttimeout(1)
  self.sock = socket.socket()
  try:
    start_time = time.time()
    self.sock.connect((self.ip,port))
    end_time = time.time()
    connect_time = int(start_time - end_time)
    info = 'Find --> [IP]: %s, [PORT]: %s, [Connect Time]: %d' % (self.ip, port, connect_time)
    print(info)
    self.__save(info)
    self.sock.close()
  except:
    # print('出错误了')
    self.sock.close()
 def __save(self,info):
  try:
    with open('results.txt', 'a') as f:
     f.write(info + '\n')
  except:
    print('写文件出现了问题')
    time.sleep(0.1)

if __name__ == '__main__':
 # 输入IP地址。
 ip = input('Input IP(example <xxx.xxx.xxx.xxx>):\n')
 print(isIP(ip))
 while not isIP(ip):
  ip = input('请输入正确的IP地址:\n')
 # 输入最小端口、
 port_min = input('需要扫描的最小端口为:')
 while not (0 <= int(port_min) < 65535):
  port_min = input('请输入正确的需要扫描的最小端口:')
 port_max = input('需要扫描的最大端口为(65535):')
 while not (0 <= int(port_min) < int(port_max) < 65535):
  port_min = input('请输入正确的需要扫描的最大端口(65535):')
 num = 8
 port_max = int(port_max)
 port_min = int(port_min)
 interval = (port_max - port_min) // num
 for i in range(interval):
  scanThread(ip, i * num, (i + 1) * num).start()

来源:https://www.cnblogs.com/cong12586/p/14135468.html

标签:python,端口,扫描
0
投稿

猜你喜欢

  • FrontPage2002简明教程二:文字与图像的处理

    2008-09-17 11:13:00
  • Mysql存储过程中游标的用法实例

    2024-01-22 14:59:10
  • IA学习笔记02:组织体系

    2009-06-12 12:22:00
  • 浅谈pytorch和Numpy的区别以及相互转换方法

    2022-02-12 03:51:47
  • 详解Python中的GIL(全局解释器锁)详解及解决GIL的几种方案

    2021-02-01 03:36:05
  • python爬取网页转换为PDF文件

    2023-02-11 08:48:24
  • MySQL Replication中的并行复制示例详解

    2024-01-29 02:49:03
  • python之json文件转xml文件案例讲解

    2021-11-18 04:54:23
  • 详解将Python程序(.py)转换为Windows可执行文件(.exe)

    2022-05-29 20:46:25
  • python人工智能自定义求导tf_diffs详解

    2023-06-11 13:31:51
  • python中filter函数的用法示例代码

    2022-11-01 19:18:40
  • python自动化测试实例解析

    2023-12-13 17:10:27
  • Python迭代用法实例教程

    2021-07-18 13:00:45
  • javascript定义变量时带var与不带var的区别分析

    2023-08-23 12:39:21
  • Python编程利用Numpy和PIL库将图片转化为手绘

    2021-09-02 07:28:55
  • OpenCV图像卷积之cv.filter2D()函数详解

    2021-11-16 07:51:34
  • 使用PIL(Python-Imaging)反转图像的颜色方法

    2022-12-15 19:16:48
  • Oracle数据安全面面观

    2010-07-27 13:27:00
  • python迭代dict的key和value的方法

    2021-11-03 16:04:17
  • Python中BeautifulSoup通过查找Id获取元素信息

    2022-12-22 22:08:59
  • asp之家 网络编程 m.aspxhome.com