mardi 1 septembre 2015

Learning Twisted - client.getPage not working

I'm following the tutorial at: http://ift.tt/1NMt3l4

I literally copy pasted finger10.py near the bottom of the page, and just changed it to a random website. API says it returns a string of the webpage, which I assume is the page source.

from twisted.internet import protocol, reactor, defer, utils
from twisted.protocols import basic
from twisted.web import client

class FingerProtocol(basic.LineReceiver):
    def lineReceived(self, user):
        d = self.factory.getUser(user)

        def onError(err):
            return 'Internal error in server'
        d.addErrback(onError)

        def writeResponse(message):
            print "test before"
            self.transport.write(message + '\r\n')
            print "test after"
            self.transport.loseConnection()
        d.addCallback(writeResponse)

class FingerFactory(protocol.ServerFactory):
    protocol = FingerProtocol

    def getUser(self, user):
        return client.getPage("www.something.com")

reactor.listenTCP(1079, FingerFactory())
reactor.run()

I run telnet localhost 1079 and get the following output:

$ telnet localhost 1079

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
hello
test before
test after
Internal error in server
Connection closed by foreign host.

Why doesn't it work?




Aucun commentaire:

Enregistrer un commentaire