python中select+setblocking和settimeout的对比

首先我设置socket为非阻塞的。然后使用select来监控套接字。

#!/usr/bin/env python
# encoding: utf-8

import socket
import threading
import Queue
import time
import select


class worker(threading.Thread):
    def __init__(self,name,queue):
        self.data=queue
        super(worker,self).__init__(name=name)
    def run(self):
        for i in range(10000):
            self.data.put(i)

class customer(threading.Thread):
    def __init__(self,name,queue):
        super(customer,self).__init__(name=name)
        self.data=queue
    def scan(self,ip,port):
        s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        s.setblocking(False)
        #s.settimeout(0.1)
        results=s.connect_ex((ip,port))
        #print results
        a,b,c=select.select([],[s,],[s,],0.1)        #设置超时时间0.1秒,当connect连接成功的时候,代表Tcp3次握手完成,此时变成可写状态
        if b:                                        #如果socket可写,代表了端口是开放的
            print port
        s.close()

    def run(self):
        while True:
            try:
                a=self.data.get(1,5)
            except:
                break
            else:
                ip='220.181.136.241'
                self.scan(ip,a)
def main():
    start=time.time()
    queue=Queue.Queue()
    pool=[]
    worke=worker('firebroo',queue)
    worke.start()
    for i in range(100):
        a=customer('firebroo',queue)
        pool.append(a)
    for i in pool:
        i.start()
    worke.join()
    for i in pool:
        i.join()
    print time.time()-start
    del pool[:]
if __name__=='__main__':
    main()

大概花费17.5秒执行完毕。下面我使用settimeout。

#!/usr/bin/env python
# encoding: utf-8

import socket
import threading
import Queue
import time
import select


class worker(threading.Thread):
    def __init__(self,name,queue):
        self.data=queue
        super(worker,self).__init__(name=name)
    def run(self):
        for i in range(10000):
            self.data.put(i)

class customer(threading.Thread):
    def __init__(self,name,queue):
        super(customer,self).__init__(name=name)
        self.data=queue
    def scan(self,ip,port):
        s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        #s.setblocking(False)
        s.settimeout(0.1)                                #为了和前面保持一致,当然得设置0.1秒
        results=s.connect_ex((ip,port))
        if results==0:                                   #connect_ex中0代表端口开放,3次握手完成
            print port
        s.close()

    def run(self):
        while True:
            try:
                a=self.data.get(1,5)
            except:
                break
            else:
                ip='220.181.136.241'
                self.scan(ip,a)
def main():
    start=time.time()
    queue=Queue.Queue()
    pool=[]
    worke=worker('firebroo',queue)
    worke.start()
    for i in range(100):
        a=customer('firebroo',queue)
        pool.append(a)
    for i in pool:
        i.start()
    worke.join()
    for i in pool:
        i.join()
    print time.time()-start
    del pool[:]
if __name__=='__main__':
    main()

时间大概是17.4。这两个测试结果应该可以说是一样的,难免会有误差。

情况看起来差不多,但是我更加推荐使用第一种做法。

转载于:https://my.oschina.net/firebroo/blog/224342

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值