dw建设网站的代码模板,肇庆市网站建设平台,网站建立需要哪些材料,手机影视网站制作用Pyhon编写一个属于自己的nmap 文章目录用Pyhon编写一个属于自己的nmap导入 socket 模块#xff0c;确定目标主机 IP 或域名以及需要扫描的端口列表开始扫描、扫描每个端口创建一个 socket 对象#xff0c;用于建立 TCP 连接尝试连接目标主机的指定端口如果连接成功#xf…用Pyhon编写一个属于自己的nmap 文章目录用Pyhon编写一个属于自己的nmap导入 socket 模块确定目标主机 IP 或域名以及需要扫描的端口列表开始扫描、扫描每个端口创建一个 socket 对象用于建立 TCP 连接尝试连接目标主机的指定端口如果连接成功则表示该端口是开放的如果用户按下键盘中断键退出程序如果无法解析主机名退出程序如果连接失败退出程序完整代码附上要编写一个类似于nmap的端口扫描工具我们需要
确定要扫描的IP地址或主机名。确定要扫描的端口范围。创建一个Socket对象使用Socket对象与目标主机建立TCP连接。使用Socket对象发送TCP SYN包到目标主机上指定的端口。接收目标主机返回的TCP SYN/ACK包。根据目标主机返回的包判断是否该端口处于打开状态。
重复步骤4-6直到扫描完所有指定的端口。接着输出扫描结果。
导入 socket 模块确定目标主机 IP 或域名以及需要扫描的端口列表
import socketclass PortScanner:def __init__(self, target, ports):self.target targetself.ports ports开始扫描、扫描每个端口
def scan(self):print(fStarting scan on {self.target}...\n)for port in self.ports:self.scan_port(port)创建一个 socket 对象用于建立 TCP 连接 sock socket.socket(socket.AF_INET, socket.SOCK_STREAM)sock.settimeout(0.5)尝试连接目标主机的指定端口
result sock.connect_ex((self.target, port))if result 0:如果连接成功则表示该端口是开放的 print(fPort {port} is open.)如果用户按下键盘中断键退出程序
print(Exiting...)exit()如果无法解析主机名退出程序
print(Hostname could not be resolved. Exiting...)exit()如果连接失败退出程序 print(Could not connect to server. Exiting...)exit()完整代码附上
import socketclass PortScanner:def __init__(self, target, ports):self.target targetself.ports portsdef scan(self):print(fStarting scan on {self.target}...\n)for port in self.ports:self.scan_port(port)def scan_port(self, port):try:sock socket.socket(socket.AF_INET, socket.SOCK_STREAM)sock.settimeout(0.5)result sock.connect_ex((self.target, port))if result 0:print(fPort {port} is open.)sock.close()except KeyboardInterrupt:print(Exiting...)exit()except socket.gaierror:print(Hostname could not be resolved. Exiting...)exit()except socket.error:print(Could not connect to server. Exiting...)exit()