python实现多线程端口扫描

yipeiwu_com6年前Python基础

一个简易的TCP端口扫描器,使用python3实现。

需求:扫描目标网站开放哪些端口号,将所有开放的端口号输出。

分析:使用socket连接,如果连接成功,认为端口开放,如果连接失败,认为端口关闭(有可能端口开放但连接失败,这里简单认为端口不开放)

使用到的库:socket, threading

过程:

先定义一个函数,对给定的(ip, port)进行扫描,看其是否能连接成功。

def tcpPortScan(ip, port, openPort):
 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 创建套接字
 sock.settimeout(0.1)   # 设置延时时间
 try:
  result = sock.connect_ex((ip, port))
  if result == 0:    # 如果连接成功,返回值为0
   openPort.append(port) # 如果端口开放,就把端口port赋给openPort
 except:
  pass
 sock.close()     # 关闭套接字

当需要扫描目标地址的多个端口时,循环使用上述函数的话,扫描速度会极其慢,因为考虑使用多线程。

再定义一个函数,实现多线程扫描。

def threadingPortScan(host, portList, openPorts = []):
 
 hostIP = socket.gethostbyname(host) # 获取域名对应的IP地址
 nloops = range(len(portList))
 threads = []
 
 for i in nloops:
  t = threading.Thread(target=tcpPortScan, args=(hostIP, portList[i], openPorts))
  threads.append(t)
 
 for i in nloops:
  threads[i].start()
 
 for i in nloops:
  threads[i].join()
 return openPorts  # 返回值为该域名下开放的端口列表

完整代码如下:

# -*- coding:utf-8 -*-
'''
使用多线程,检测一个目标地址的端口开放情况,目标地址由用户输入,端口暂时定义为0~1024,
检测TCP连接是否成功,如果连接成功,则端口开放,不成功则端口关闭
'''
 
import socket
import threading
 
def main():
 host = input('please input domain:')
 portList = range(0, 1025)
 openPorts = threadingPortScan(host, portList)
 print(host,'open ports:', openPorts)
 
# 对给定的(ip, port)进行TCP连接扫描
def tcpPortScan(ip, port, openPort):
 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 创建套接字
 sock.settimeout(0.1)   # 设置延时时间
 try:
  result = sock.connect_ex((ip, port))
  if result == 0:
   openPort.append(port) # 如果端口开放,就把端口port赋给openPort
 except:
  pass
 sock.close()     # 关闭套接字
 
 
def threadingPortScan(host, portList, openPorts = []):
 
 hostIP = socket.gethostbyname(host) # 获取域名对应的IP地址
 nloops = range(len(portList))
 threads = []
 
 for i in nloops:
  t = threading.Thread(target=tcpPortScan, args=(hostIP, portList[i], openPorts))
  threads.append(t)
 
 for i in nloops:
  threads[i].start()
 
 for i in nloops:
  threads[i].join()
 return openPorts  # 返回值为该域名下开放的端口列表
 
if __name__ == '__main__':
 main()

使用www.qq.com做一个测试,测试结果如下:

>>>please input domain: www.qq.com
www.qq.com open ports: [80, 843]

总结:这个小程序仅适用于新手练习,不适合真正应用。该简易端口扫描器仅能扫描出一部分端口,有些端口可能因为防火墙拦截导致扫描失败。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python通过pil模块将raw图片转换成png图片的方法

本文实例讲述了python通过pil模块将raw图片转换成png图片的方法。分享给大家供大家参考。具体分析如下: python通过pil模块将raw图片转换成png图片,pil中包含了f...

Python实现Const详解

python语言本身没有提供const,但实际开发中经常会遇到需要使用const的情形,由于语言本身没有这种支出,因此需要使用一些技巧来实现这一功能 定义const类如下 复制代码 代码...

python的schedule定时任务模块二次封装方法

通过定时来执行任务,我们日常工作生活中会经常用到。python有schedule这个库,简单好用,比如,可以每秒,每分,每小时,每天,每天的某个时间点,间隔天数的某个时间点定时执行,另外...

基于python 字符编码的理解

一、字符编码简史: 美国:1963年 ASCII (包含127个字符  占1个字节) 中国:1980年 GB2312 (收录7445个汉字,包括6763个汉字和682个其它符号...

Python的内存泄漏及gc模块的使用分析

一般来说在 Python 中,为了解决内存泄漏问题,采用了对象引用计数,并基于引用计数实现自动垃圾回收。 由于Python 有了自动垃圾回收功能,就造成了不少初学者误认为自己从此过上了好...