[Twisted-Python] Novice question on twisted framework

Sury Soni ssoni at nextdigital.com
Mon Mar 16 22:18:31 MDT 2009


Hi,

 

I have an existing application, written using Ice Middleware
(www.zeroc.com)

 

This application is my source of infinite queue (server)

 

Following is my application (processing client to my infinite queue
server) idea to be implemented using Twisted Framework.

 

I can loop through my infinite queue and receive email data (Using
.next() kind of function)

 

Using this email data object, I need to trigger event to send email
using ESMTPSenderFactory and ESMTPSender. As, later I would be extending
ESMTP* classes to do my little funky stuff.

 

What is the best code design I can use to implement this stuff.

 

Following is basic algorithm kind of code I am doing right now, but I
feel I am missing something.

 

#================

#Code Begins Here: .tac file

#================

 

import StringIO

 

from twisted.application import service 

 

from twisted.application import internet

from twisted.internet import protocol, defer

from twisted.mail import smtp, relaymanager

from twisted.internet import reactor

 

Def process_queue:

    While True:

         Try:

                Data = my_queue.next()

 

                getMailExchange('localhost).addCallback(cbMailExchange,
data)         

       Except:

                Break

    Reactor.stop()

 

 

reactor.callLater(0, process_queue)

 

 

application = service.Application("SMTP Client Tutorial")

 

class SMTPTutorialClient(smtp.ESMTPClient):

    mailFrom = "test at test.com"

    mailTo = "receiver at test.com"

    mailData = '''\

Date: Fri, 6 Feb 2004 10:14:39 -0800

From: Test <test at test.com>

To: Receiver <receiver at test.com>

Subject: Test Mail!

 

Hello, how are you, goodbye.

 

'''

    

    def __init__(self, secret="", identity="", data = ""):

        smtp.ESMTPClient.__init__(self, secret=secret,
identity=identity)

        self.secret = secret

        self.identity = identity

        self.data = data

 

    def getMailFrom(self):

        result = self.mailFrom

        self.mailFrom = None

        return result

    

    def getMailTo(self):

        return [self.mailTo]

 

    def getMailData(self):

        return StringIO.StringIO(self.mailData+self.data)

 

    def sentMail(self, code, resp, numOk, addresses, log):

        print 'Sent', numOk, 'messages'

        #reactor.stop()

 

def getMailExchange(host):

    def cbMX(mxRecord):

        return str(mxRecord.name)

    return relaymanager.MXCalculator().getMX(host).addCallback(cbMX)

 

class SMTPClientFactory(protocol.ClientFactory):

    protocol = SMTPTutorialClient

    def __init__(self, data):

        self.data = data

        

    def buildProtocol(self, addr):

        return self.protocol(secret=None, identity='example.com',
data=self.data)

 

def cbMailExchange(exchange, data):

    smtpClientService = internet.TCPClient(exchange, 2500,
SMTPClientFactory(data))

    smtpClientService.setServiceParent(application)

    #reactor.run()

 

reactor.run()

 

#================

#Code Ends Here

#================

 

I have couple of #reactor.run() commented code, I feel like lost between
application and reactor based code.

 

My understanding is that function process_queue drives the control flow
of my application. Please correct me.

 

What is happening is, when my loop is finished in function
process_queue, then only actual events of SMTP factory and protocol
objects are created.

 

Therefore, I am not getting event driven code, which is the beauty of
Twisted Framework.

 

Please advice the best document to look and fix this code.

 

Thanks.

____________________________________
Sury Prakash Soni
Developer
____________________________________

Next Digital
Level 8, 15 William St, Melbourne 
VIC 3000 Australia
p +61 3 8612 6888
f  +61 3 8612 6899
m 0433 661 327
ssoni at nextdigital.com <mailto:ssoni at nextdigital.com> 
www.nextdigital.com <http://www.nextdigital.com/> 
____________________________________

This email and any attachments are intended only for the use of the
recipient and may be confidential and/or legally privileged. Next
Digital Group Pty Ltd ("Next Digital") disclaims liability for any
errors, omissions, viruses, loss and/or damage arising from using,
opening or transmitting this email. If you are not the intended
recipient you must not use, interfere with, disclose, copy or retain
this email and you should notify the sender immediately by return email
or by contacting Next Digital by telephone on +61 3 8612 6888.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20090317/8882219e/attachment.html>


More information about the Twisted-Python mailing list