Twisted初试

  最近一段时间在看Twisted这个框架,一个相当强大的网络编程模块!
  由于工作的原因,需要对某些IP地址列表做写扫描,在未接触Twisted框架之前,我使用的是下面的代码!
#!/usr/bin/python
import imaplib
try:
    scanlines = open("143iplist.txt")
    outfd = open("Scan-143_out.txt", 'a')

    for scanline in scanlines.readlines():
        try:
            scanip = scanline.strip().split()[0]
            print scanip
            p = imaplib.IMAP4(scanip)
            putsession = scanip + " "  + str(p.port) + " " + p.welcome+ '\n'
            outfd.write(putsession)
        except:
            pass
except  IOError:
    print "open failed"
finally:
    scanlines.close()
    outfd.close()
    print 'Task is over!'

try:
    pass
except Exception, e:
    raise
else:
    pass
finally:
    pass
  就只是单纯的使用了python的imaplib模块,事实上,扫描的IP多达百万,用这个脚本,扫描是不行的,最多也就只能扫描到几百条,而采用了异步思想的Twisted后:
#!/usr/bin/python

# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.


from twisted.internet.protocol import ClientFactory
from twisted.protocols.basic import LineReceiver
from twisted.internet import defer, task
import sys
from twisted.internet import reactor
reactor.suggestThreadPoolSize(50000)
#############################################
class IMAPClient(LineReceiver):
    def lineReceived(self, line):
        ip =  self.transport.getPeer().host,
        self.transport.loseConnection()
        self.factory.scanFinished(ip[0] + " " + line)

#############################################
class IMAPClientFactory(ClientFactory):
    protocol = IMAPClient
    def __init__(self, deferred):
        self.deferred = deferred

    def scanFinished(self, banner):
        if self.deferred is not None:
            d, self.deferred = self.deferred, None
            d.callback(banner)

    def clientConnectionFailed(self, connector, reason):
        if self.deferred is not None:
            d, self.deferred = self.deferred, None
            d.errback(reason)
#############################################
succeFD = open("output.txt", 'a')
faildFD = open("failedoutput.txt", 'a')
def getResult(result,mx):
    print mx,result
    succeFD.write(mx +' ' + result + '\n')
    succeFD.flush()
def getError(reason,mx):
err = reason.getErrorMessage()
    faildFD.write(err + '\n')
    faildFD.flush()

#############################################
def doWork():
    i = 1
    fp = file("Sendmailiplist.log", 'a')
    for mx in file("SendmailIPlist.txt",'r'):
        mx = mx.strip().split()[0]
        d = defer.Deferred()
        factory = IMAPClientFactory(d)
        reactor.connectTCP(mx, 143, factory)
        d.addCallback(getResult,mx)
        d.addErrback(getError,mx)
        logRecord = "%d\t%s\n" %(i, mx)
        i += 1
        fp.write(logRecord)
        fp.flush()
        yield d


def finish(igo):
    reactor.callLater(5, reactor.stop)


#############################################
def taskRun():
    deferreds = []
    coop = task.Cooperator()
    work = doWork()
    maxRun = 50000
    for i in xrange(maxRun):
        d = coop.coiterate(work)
        deferreds.append(d)
    dl = defer.DeferredList(deferreds)
    dl.addCallback(finish)


#############################################
def main():
    taskRun()
    reactor.run()
    succeFD.close()
    faildFD.close()

if __name__ == '__main__':
    main()
  只要只分钟就能完成刚刚的巨大的任务!

你可能感兴趣的:(Twisted初试)