[Twisted-web] reactor.listenSSL() question

vitaly at synapticvision.com vitaly at synapticvision.com
Tue Apr 21 13:47:06 EDT 2009


Hi,
I'm trying to make work the following client/server, but the
server always return: HTTP/1.1 400 Bad Request.
The example server from Twisted.com(echoserv_ssl.py) working fine
but it uses:
factory = Factory()
factory.protocol = echoserv.Echo

Appreciate any help!!!

---------
server.py
---------
from twisted.internet import reactor, defer, ssl
from twisted.web import server, static, resource
from OpenSSL import SSL
from pyamf.remoting.gateway.twisted import TwistedGateway
from pyamf.remoting.gateway import expose_request

class FlexInterface():
      ...

class ServerContextFactory:

      # server.key and server.crt files existing and are real  
certificate files.
      def getContext(self):

          ctx = SSL.Context(SSL.SSLv23_METHOD)
          ctx.use_certificate_file('server.crt')
          ctx.use_privatekey_file('server.key')
          return ctx


if __name__ == "__main__":

      gateway = TwistedGateway({ "controller": FlexInterface() },
expose_request=False)

      root = resource.Resource()
      root.putChild('', gateway)

      reactor.listenSSL(8050, server.Site(root), ServerContextFactory())
      reactor.run()


---------
client.py
---------
#!/usr/bin/python
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
# See LICENSE for details.


from OpenSSL import SSL
import sys

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


class EchoClient(LineReceiver):
      end="Bye-bye!"
      def connectionMade(self):
          self.sendLine("Hello, world!")
          self.sendLine("What a fine day it is.")
          self.sendLine(self.end)

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

      def lineReceived(self, line):
          print "receive:", line
          if line==self.end:
              self.transport.loseConnection()
              print "Connection closed"

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', 8050, factory,
ssl.ClientContextFactory(), timeout=3)
      reactor.run()

if __name__ == '__main__':
      main()


----- End forwarded message -----




More information about the Twisted-web mailing list