python多线程扫描端口示例

 更新时间:2014年01月16日 14:25:54   作者:  
这篇文章主要介绍了python多线程扫描端口示例,大家参考使用吧

复制代码 代码如下:

# -*- 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
'''

相关文章

  • 利用Python进行图像的加法,图像混合(附代码)

    利用Python进行图像的加法,图像混合(附代码)

    这篇文章主要介绍了利用Python进行图像的加法,图像混合(附代码),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • 没有安装Python的电脑运行Python代码教程

    没有安装Python的电脑运行Python代码教程

    你有没有遇到过这种情况,自己辛苦码完了代码想发给别人运行看效果,可是对方竟然没安装Python,这要怎么运行呢?本篇文章带你解决这个问题,需要的朋友快来看看
    2021-10-10
  • pandas的Series类型与基本操作详解

    pandas的Series类型与基本操作详解

    这篇文章主要介绍了pandas的Series类型与基本操作详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • Python实现曲线点抽稀算法的示例

    Python实现曲线点抽稀算法的示例

    本篇文章主要介绍了Python实现曲线点抽稀算法的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • Python协程异步爬取数据(asyncio+aiohttp)实例

    Python协程异步爬取数据(asyncio+aiohttp)实例

    这篇文章主要为大家介绍了Python协程异步爬取数据(asyncio+aiohttp)实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • Python 按比例获取样本数据或执行任务的实现代码

    Python 按比例获取样本数据或执行任务的实现代码

    这篇文章主要介绍了Python 按比例获取样本数据或执行任务,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • python开发之IDEL(Python GUI)的使用方法图文详解

    python开发之IDEL(Python GUI)的使用方法图文详解

    这篇文章主要介绍了python开发之IDEL(Python GUI)的使用方法,结合图文形式较为详细的分析总结了Python GUI的具体使用方法,需要的朋友可以参考下
    2015-11-11
  • Python编程中flask的简介与简单使用

    Python编程中flask的简介与简单使用

    今天小编就为大家分享一篇关于Python编程中flask的简介与简单使用,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • python PaddleOCR库用法及知识点详解

    python PaddleOCR库用法及知识点详解

    在本篇内容里小编给大家分享的是一篇关于python PaddleOCR库用法及知识点详解内容,对此有需要的朋友们可以学习参考下。
    2021-07-07
  • Python简单计算文件MD5值的方法示例

    Python简单计算文件MD5值的方法示例

    这篇文章主要介绍了Python简单计算文件MD5值的方法,涉及Python文件读取、hash运算及md5加密等相关操作技巧,需要的朋友可以参考下
    2018-04-04

最新评论