python 实现端口扫描工具
作者:lucky_qi 发布时间:2022-12-05 23:02:45
标签:python,端口,扫描
# 简单的端口扫描工具
# 作者: 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语句mode = ‘test’ if y is None else &lsquo
- PyQt5单选按钮控件QRadioButton简介QRadioButton 继承自 QAbstractButton,其主要作用提供用户一些互
- 需要安装pywin32模块,pip install pywin32##pip install pywin32import win32api,
- 新安装的MySQL5.7,登录时提示密码错误,安装的时候并没有更改密码,后来通过免密码登录的方式更改密码,输入update mysql.us
- Python具有基本的文本文件读写功能。Python的标准库提供有更丰富的读写功能。文本文件的读写主要通过open()所构建的文件对象来实现
- 前言在对DataFrame数据进行处理时,存在需要对数据内容进行遍历的场景。因此记录一下按照行,列遍历的几种方式。一、按行遍历1. 使用lo
- 前言在《设计模式》一书中工厂模式提到了:工厂方法模式(Factory Method)抽象工厂模式 (Abstract Factory)但是在
- 一、按索引取数据①tf.gather()输入参数:数据、维度、索引例:设数据是[4,35,8],4个班级,每个班级35个学生,每个学生8门课
- 本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下#!/usr/bin/env python# -*- c
- php操作xml最近计划写个人的小网站,一系列原因选择了用php来写,最大的问题就是虽然php很流行,但我从来没有接触过php,看了一个多星
- CentOS 7上PHP默认是以apache或者nobody的身份运行的,这种方式下由于PHP运行需要的权限比较大,会有安全隐患,还可能会受
- 考虑以下python程序:#!/usr/bin/env pythonimport syssys.stdout.write("std
- 本文实例为大家分享了pygame实现俄罗斯方块游戏的具体代码,基础的第一篇,供大家参考,具体内容如下一、初始界面之前的游戏都比较简单,所以代
- 废话不多说,直接上代码!# coding:utf-8from multiprocessing import Poolimport timed
- Accessibility—-可访问性/无障碍访问Accessibility在此处指网页的可访问性。W3C的网页可访问性定义如下:“网页可访
- 原文:http://www.smashingmagazine.com/ ... emarkable-favicons/翻译:Blank(怿飞
- 本文讲述使用JSP实现用户登录,包括用户登录、注册和退出功能等。1.系统用例图2.页面流程图3.数据库设计本例使用oracle数据库创建用户
- 将通过各种例子来了解如何在 Python 中使用 type() 函数。你好类型打印 "Hello World "几乎是你
- 官方网站:http://www.extjs.com/演示地址:http://www.yaohaixiao.com/effects/sampl
- 刚来这个公司,熟悉了环境,老大就开始让我做一个迁移、修改代码的工作,我想说的是,这种工作真没劲~~,看别人的代码、改别人的代码、这里改个变量