[Twisted-Python] twisted threading server has to be stopped with KILL

snacktime snacktime at gmail.com
Wed Feb 9 01:40:11 EST 2005


>  Complete examples are preferable.  If the problem cannot be reproduced
> (that is to say, the source copied into a file, the file run with Python,
> and the undesirable behavior observed as a result), it is much more time
> consuming to attempt to provide a useful response.
> 
Ok here is a test server/client.  

Server:
--------------------------
from twisted.application import service,internet
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import defer, reactor
from twisted.python import threadable
from twisted.internet import threads
from twisted.python import log
threadable.init(1)
import sys
from OpenSSL import SSL

class ServerContextFactory:

    def getContext(self):
        """Create an SSL context.

        This is a sample implementation that loads a certificate from a file
        called 'server.pem'."""
        ctx = SSL.Context(SSL.SSLv23_METHOD)
        ctx.use_certificate_file('server.pem')
        ctx.use_privatekey_file('server.pem')
        return ctx

class ProcessTransaction:

  def Do(self,data):
      return 'Done'


### Protocol Implementation

class OT(Protocol):
    def dataReceived(self, data):
      """As soon as any data is received, process it in a thread."""
      reactor.callLater(0, self.Start,data)

    def PrintData(self,data):
      self.transport.write("%s\r\n" % data)
      self.transport.loseConnection()

    def Start(self,data):
      c = ProcessTransaction()
      d = threads.deferToThread(c.Do,data)
      d.addCallback(self.PrintData)
      d.addErrback(log.err)


application = service.Application("otransact")
OTService = service.IServiceCollection(application)
OTfactory = Factory()
OTfactory.protocol = OT
OTServer = internet.SSLServer(8000, OTfactory,ServerContextFactory())
OTServer.setServiceParent(OTService)

Client:
-----------------------
from OpenSSL import SSL
import sys
import time

from twisted.internet.protocol import ClientFactory
from twisted.protocols.basic import LineReceiver
from twisted.internet import ssl, reactor

class EchoClient(LineReceiver):
    end="GoodBye"
    def connectionMade(self):
        self.sendLine("Testing")

    def connectionLost(self, reason):
        print 'connection lost (protocol)'
        reactor.stop()

    def lineReceived(self, line):
        print "receive:", line
        self.transport.loseConnection()

class EchoClientFactory(ClientFactory):
    protocol = EchoClient

    def clientConnectionFailed(self, connector, reason):
        print 'connection failed:', reason.getErrorMessage()
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        print 'connection lost:', reason.getErrorMessage()
        reactor.stop()


def main():
    factory = EchoClientFactory()
    reactor.connectSSL('localhost', 8000, factory, ssl.ClientContextFactory())
    reactor.run()


if __name__ == '__main__':
    main()




More information about the Twisted-Python mailing list