python多线程扫描端口示例

时间:2022-06-28 23:18:58 


# -*- coding: cp936 -*-
import socket
from threading import Thread,activeCount,Lock
from time import ctime
mutex = Lock()

class Loop(Thread):
    def __init__(self,ip,port,que):
        Thread.__init__(self)
        self.ip     = ip
        self.port   = port
        self.que    = que

    def run(self):
        global mutex
        try:
            client = socket.socket()
            indicator = client.connect_ex((self.ip,self.port))
            if mutex.acquire(1):
                if indicator == 0:
                    que.append(self.ip+'\t'+str(self.port))
                else:
                    print self.ip,'\t',str(self.port),'不可达'
                mutex.release()
        except:
            if mutex.acquire(1):
                print self.ip,'\t',str(self.port),'不可达'
                mutex.release()

class Main(Thread):
    def __init__(self,ip,que):
        Thread.__init__(self)
        self.ip  = ip
        self.que = que

    def run(self):
        i = 0
        while i < 65536:
            if activeCount() <= 200:
                Loop(ip=self.ip,port=i,que=self.que).start()
                i = i + 1

if __name__ == '__main__':
    que = []
    ip = raw_input('IP=')

    main = Main(ip = ip,que = que)
    main.start()

    while True:
        if activeCount() <= 1 and main.isAlive() == False:
            break

    print ''
    f = open('portOpen.py','a')
    f.write("'''")
    f.write(ctime()+'\n')
    f.flush()
    for i in range(0,len(que)):
        print que[i]
        f.write('\t'+que[i]+'\n')
        f.flush()
    f.write("'''")
    f.close()

    raw_input()

'''Mon Jan 13 07:12:53 2014
 localhost 135
 localhost 1028
 localhost 8048
 localhost 8080
 localhost 8181
 localhost 8730
 localhost 12040
 localhost 12897
 localhost 18040
 localhost 18611
''''''Tue Jan 14 10:04:58 2014
 localhost 135
 localhost 1028
 localhost 8048
 localhost 8080
 localhost 8181
 localhost 12897
 localhost 18040
 localhost 18611
'''

标签:多线程,扫描端口
0
投稿

猜你喜欢

  • 一些实用的sql语句

    2009-11-10 20:21:00
  • 如何判断SQL语句是否执行了?

    2010-01-12 20:03:00
  • Python下的Softmax回归函数的实现方法(推荐)

    2022-02-13 10:42:17
  • 解析arp病毒背后利用的Javascript技术

    2007-08-08 09:55:00
  • FCKeditor技巧之在按钮旁边加文字

    2007-10-10 13:17:00
  • OpenCV凸包检测和凸缺陷学习示例

    2021-08-20 15:26:20
  • python使用pandas读xlsx文件的实现

    2021-01-21 05:00:10
  • IE6 bug: 消失的绝对定位元素

    2009-12-04 12:11:00
  • 天极网页版式设计的思考

    2008-01-18 12:44:00
  • Python+Pygame实现神庙逃亡游戏

    2022-06-12 16:26:28
  • javabean servlet jsp实现分页功能代码解析

    2023-06-13 15:21:24
  • 利用ASP输出excel文件一例

    2008-06-06 13:18:00
  • 从一道js笔试题到==运算符的简析

    2010-05-10 20:28:00
  • 好的产品设计并非始于图片,而是对人的理解

    2009-08-02 20:25:00
  • 正则的replace方法(产生的字符串副本)

    2008-06-03 13:31:00
  • 工程师必须了解的LRU缓存淘汰算法以及python实现过程

    2023-05-22 22:46:00
  • Linux+php+apache+oracle环境搭建之CentOS下安装Oracle数据库

    2023-10-08 01:02:56
  • 自然描述与自然任务

    2010-01-26 15:51:00
  • 登录与注册两者的距离

    2009-01-02 16:48:00
  • python中实现数组和列表读取一列的方法

    2021-04-07 08:04:48
  • asp之家 网络编程 m.aspxhome.com