[Twisted-Python] using ClientFactory from within a server

Jp Calderone exarkun at intarweb.us
Sun Sep 28 23:23:08 EDT 2003

On Sun, Sep 28, 2003 at 09:30:33PM -0400, Greg wrote:
> I am having trouble using ClientFactory to communicate from within a server. 
> Specifically, I can't figure out the proper way to exit from the client 
> process. 
> Included is some example code illustrating the problem I am having. It is the 
> simple echo server example, but when someone sends some text to the server, I 
> am trying to talk to an SMTP server to send an email. This is just an example 
> (there are probably better ways to fire off an email), but you get the idea. 
> The client code never "exits" and if I try to use reactor.stop() it causes a 
> traceback. 

  reactor.run() enters the twisted mainloop.  reactor.stop() causes the
mainloop to terminate in the near future.  You cannot nest calls to the
mainloop (nor do you need to do so).

> So how do I properly exit from this client code, or (being new to twisted) am 
> I going about this in the entirely wrong way? Thanks.
> #!/usr/bin/env python
> from twisted.internet import reactor
> from twisted.internet.protocol import Protocol, ClientFactory, ServerFactory
> import sys
> class Echo(Protocol):
> 	"""This is just about the simplest possible protocol"""
> 	def dataReceived(self, data):
> 		"As soon as any data is received, write it back."
> 		self.transport.write(data)
> 		factory2 = MySMTPClientFactory()
> 		host = 'localhost'
> 		port = 25
> 		reactor.connectTCP(host, port, factory2)
> 		reactor.run()

  Remove the above call to "reactor.run() and see what happens.

> 		print "Returned from sending email"
> 		# ^ Never gets to this point
> class MySMTP(Protocol):
> [snip]

  I don't understand why you aren't using twisted.protocols.smtp.

> [snip]


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20030928/0421287f/attachment.pgp 

More information about the Twisted-Python mailing list