python实现的多线程端口扫描功能示例

yipeiwu_com5年前Python基础

本文实例讲述了python实现的多线程端口扫描功能。分享给大家供大家参考,具体如下:

下面的程序给出了对给定的ip主机进行多线程扫描的Python代码

#!/usr/bin/env python
#encoding: utf-8
import socket, sys, thread, time
openPortNum = 0
socket.setdefaulttimeout(3)
def usage():
  print '''''Usage:
  Scan the port of one IP: python port_scan_multithread.py -o <ip>
  Scan the port of one IP: python port_scan_multithread.py -m <ip1, ip2, ip3, ip4 ...>
  '''
  print 'Exit'
  sys.exit(1)
def socket_port(ip, PORT):
  global openPortNum
  if PORT > 65535:
    print 'Port scanning beyond the port range, interrupt to scan'
    sys.exit(1)
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  result = s.connect_ex((ip, PORT))
  if(result == 0):
    print ip, PORT,'is open'
    openPortNum += 1
  s.close()
def start_scan(IP):
  for port in range(0, 65535+1):
    thread.start_new_thread(socket_port, (IP, int(port)))
    time.sleep(0.006)
if __name__ == '__main__':
  t = 0
  if len(sys.argv)<2 or sys.argv[1] == '-h':
    usage()
  elif sys.argv[1] == '-o':
    ONE_IP = raw_input('Please input ip of scanning: ')
    t = time.time()
    start_scan(ONE_IP)
  elif sys.argv[1] == '-m':
    MANY_IP = raw_input('Please input many ip of scanning: ')
    IP_SEG = MANY_IP.split(',')
    t = time.time()
    for i in IP_SEG:
      start_scan(i)
  print
  print 'total open port is %s, scan used time is: %f ' % (openPortNum, time.time()-t)

运行效果图

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python URL操作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

Python3指定路径寻找符合匹配模式文件

本文实例讲述了Python3指定路径寻找符合匹配模式文件。分享给大家供大家参考。具体实现方法如下: 这里给定一个搜索路径,需要在此目录中找出所有符合匹配模式的文件 import g...

python使用arcpy.mapping模块批量出图

出图是项目里常见的任务,有的项目甚至会要上百张图片,所以批量出土工具很有必要。arcpy.mapping就是ArcGIS里的出图模块,能快速完成一个出图工具。 arcpy.mapping...

python使用openpyxl库修改excel表格数据方法

python使用openpyxl库修改excel表格数据方法

1、openpyxl库可以读写xlsx格式的文件,对于xls旧格式的文件只能用xlrd读,xlwt写来完成了。 简单封装类: from openpyxl import load_wo...

python使用mysql数据库示例代码

一,安装mysql 如果是windows 用户,mysql 的安装非常简单,直接下载安装文件,双击安装文件一步一步进行操作即可。 Linux 下的安装可能会更加简单,除了下载安装包进...

python分治法求二维数组局部峰值方法

python分治法求二维数组局部峰值方法

题目的意思大致是在一个n*m的二维数组中,找到一个局部峰值。峰值要求大于相邻的四个元素(数组边界以外视为负无穷),比如最后我们找到峰值A[j][i],则有A[j][i] > A[j...