[Twisted-Python] Problem with ClientFactory

Michal Chruszcz troll at pld-linux.org
Wed Jan 26 10:57:14 EST 2005


Hi,

I've been writing a small client program connecting to the server through 
SSL when occurred a strange problem. At the beginning I thought that it was 
my fault, I changed my mind after examining examples (exactly 
echoclient_ssl.py) shipped with the Twisted distribution, though. After 
many try-and-fails I think I found the problematic code. I created this 
test case to show the issue:
#v+
from OpenSSL import SSL
from twisted.internet.protocol import ClientFactory
from twisted.protocols.basic import LineOnlyReceiver

class Protocol(LineOnlyReceiver):
    def connectionMade(self):
        self.sendLine("GET / HTTP/1.0\n")

    def lineReceived(self, line):
        print line

class Factory(ClientFactory):
    protocol = Protocol
    def startedConnecting(self, connector):
        print "Connecting..."

    def buildProtocol(self, addr):
        print "Connected."

    def clientConnectionLost(self, connector, reason):
        print "Connection Lost."
        reactor.stop()

    def clientConnectionFailed(self, connector, reason):
        print "Connection Failed."
        reactor.stop()

if __name__ == "__main__":
    host = "sf.net"
    port = 443
    from twisted.internet import reactor, ssl
    reactor.connectSSL(host, port, Factory(), ssl.ClientContextFactory())
    reactor.run()
#v-

After running this code I get "Connecting, Connected" lines and long 
tracebacks warning about two exceptions (I could attach it, if it was 
useful).

$ rpm -q python-Twisted python-pyOpenSSL; uname -o
python-Twisted-1.3.0-7
python-pyOpenSSL-0.6-2
PLD Linux

The most interesting (funny?) thing with this problem is the simplest 
workaround - after removing startedConnecting and buildProtocol methods of 
Factory class everything works fine, I get the response from the server and 
requested page. Is that meant to (not)work that way? If so, please write it 
somewhere in the HOWTO using big letters, otherwise let somebody fix 
it. ;-)

P.S. HTTPS is just an example. I know about dedicated software for that 
purpose, but it's useless for me. ;-) I'm just wondering about the cause of 
this problem.
-- 
Michal Chruszcz -=- Seen at http://1lo.sanok.pl/~troll/gallery.php




More information about the Twisted-Python mailing list