[Twisted-Python]The problem afer reactor stoped....
Hi,all I want to write a program that can send some e-mails automaticlly,i use the code like this: def sendmail(authenticationUsername, authenticationSecret,fromAddress, toAddress,messageFile,smtpHost, smtpPort=25): resultDeferred = Deferred() senderFactory = ESMTPSenderFactory(authenticationUsername,authenticationSecret,fromAddress,toAddress,messageFile,resultDeferred,requireTransportSecurity=False) reactor.connectTCP(smtpHost, smtpPort, senderFactory) return resultDeferred def sendComplete(result): print "Message sent." reactor.stop() class EmailUtil: def __init__(self,popserver,smptserver,username,password): self.popserver=popserver self.smtpserver=smptserver self.username=username self.password=password def goSend(self,fromaddr, toaddr, subject, body, filenames): message = buildMessage(fromaddr, toaddr, subject, body, filenames) messageData = message.as_string(unixfrom=False) tempfile=open('temp.mai','wb') tempfile.write(messageData) tempfile.close() print 'Sending.....' result = sendmail(self.username,self.password,fromaddr,toaddr,file('temp.mai'),self.smtpserver) result.addCallbacks(sendComplete,handleError) reactor.run() if __name__=='__main__': f=EmailUtil('','smtp.xxx.com','xxxx','1234') filenames=['test2.docx'] f.goSend('xxxx@xxx.com','xxx@hotmail.com','testSend','aaaaaaaaaa',filenames) f.goSend('xxxx@xxx.com','xxx@hotmail.com','testSend','aaaaaaaaaa',filenames) f.goSend('xxxx@xxx.com','xxx@hotmail.com','testSend','aaaaaaaaaa',filenames) I try to send the same mail,but after the first mail was sended,the program deadlock,and i use some tool konws after the first mail sended,no packets was send to the outside world.I guess maybe the problem related with the reactor run and stop,but i am sure,any one can tell me what's problem is?Thanks
李祥曦 wrote:
Hi,all
Hi
I want to write a program that can send some e-mails automaticlly,i use the code like this:
reactor.stop() and reactor.run() have to be called only one time! Try the following: def sendmail(authenticationUsername, authenticationSecret,fromAddress, ... def sendComplete(result): print "Message sent." # reactor.stop() comment this line out! class EmailUtil: def __init__(self,popserver,smptserver,username,password): ... def goSend(self,fromaddr, toaddr, subject, body, filenames): ... return result.addCallbacks(sendComplete,handleError) # get back the result # reactor.run() comment this line out if __name__=='__main__': twisted.internet.defer import DeferredList f=EmailUtil('','smtp.xxx.com','xxxx','1234') filenames=['test2.docx'] dd1 = f.goSend('xxxx@xxx.com','xxx@hotmail.com','testSend','aaaaaaaaaa',filenames) dd2 = f.goSend('xxxx@xxx.com','xxx@hotmail.com','testSend','aaaaaaaaaa',filenames) dd3 = f.goSend('xxxx@xxx.com','xxx@hotmail.com','testSend','aaaaaaaaaa',filenames) d4ll =DeferredList([dd1,dd2,dd3]) #errback ? def stop(*arg): reactor.stop() d4ll.addCallback(stop) reactor.run() Note: not tested You can test EmailUtil alone without twisted, removing his internal addCallbacks (you can attach the callback in the main) Alessandro
Very appreciate your help,It can worked,but i want know if it not
possible to run then stop then run reactor again?
2008/5/6 Alessandro
李祥曦 wrote:
Hi,all
Hi
I want to write a program that can send some e-mails automaticlly,i use the code like this:
reactor.stop() and reactor.run() have to be called only one time!
Try the following:
def sendmail(authenticationUsername, authenticationSecret,fromAddress, ...
def sendComplete(result): print "Message sent." # reactor.stop() comment this line out!
class EmailUtil: def __init__(self,popserver,smptserver,username,password): ...
def goSend(self,fromaddr, toaddr, subject, body, filenames): ... return result.addCallbacks(sendComplete,handleError) # get back the result # reactor.run() comment this line out
if __name__=='__main__': twisted.internet.defer import DeferredList
f=EmailUtil('','smtp.xxx.com','xxxx','1234') filenames=['test2.docx'] dd1 = f.goSend('xxxx@xxx.com','xxx@hotmail.com','testSend','aaaaaaaaaa',filenames) dd2 = f.goSend('xxxx@xxx.com','xxx@hotmail.com','testSend','aaaaaaaaaa',filenames) dd3 = f.goSend('xxxx@xxx.com','xxx@hotmail.com','testSend','aaaaaaaaaa',filenames) d4ll =DeferredList([dd1,dd2,dd3]) #errback ?
def stop(*arg): reactor.stop()
d4ll.addCallback(stop) reactor.run()
Note: not tested
You can test EmailUtil alone without twisted, removing his internal addCallbacks (you can attach the callback in the main)
Alessandro
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (3)
-
Alessandro
-
Tim Allen
-
李祥曦