python多线程扫描端口示例

yipeiwu_com5年前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
'''

相关文章

tensorflow使用神经网络实现mnist分类

本文实例为大家分享了tensorflow神经网络实现mnist分类的具体代码,供大家参考,具体内容如下 只有两层的神经网络,直接上代码 #引入包 import tensorflow...

Pytorch Tensor的索引与切片例子

1. Pytorch风格的索引 根据Tensor的shape,从前往后索引,依次在每个维度上做索引。 示例代码: import torch a = torch.rand(4, 3...

CentOS7安装Python3的教程详解

打算学习linux和考一下认证。 学习HCIA-AI实验手册发现的小问题和记录贴,防止自己忘。我不知道这个手册是不是公开的,你们自己去华为下载吧 首先执行 yum -y groupi...

python 实现矩阵填充0的例子

需求: 原矩阵 [[1 2 3] [4 5 6] [7 8 9]] 在原矩阵元素之间填充元素 0,得到 [[1. 0. 2. 0. 3.] [0. 0. 0. 0. 0....

完美解决安装完tensorflow后pip无法使用的问题

Win8,ANACONDA3(64-bit),Python3.6.2。ANACONDA Prompt中不能用pip命令安装包,并且是在安装了TensorFlow后才发生的。 报错如下:...