Question

from twisted.internet.protocol import Factory,Protocol
from twisted.internet import reactor

class ChatServer(Protocol):
    def connectionMade(self):
        print("A Client Has Connected")

factory = Factory()
reactor.listenTCP(80,factory)
print("Chat Server Started")

reactor.run()

the above code is running succesfully.but when I try to open TCP(telnet localhost 80).

Errors occurs :

Unhandled Error
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\twisted\python\log.py", line 69, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "C:\Python27\lib\site-packages\twisted\python\context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "C:\Python27\lib\site-packages\twisted\python\context.py", line 81, in callWithContext
    return func(*args,**kw)
  File "C:\Python27\lib\site-packages\twisted\internet\selectreactor.py", line 150, in _doReadOrWrite
    why = getattr(selectable, method)()
--- <exception caught here> ---
  File "C:\Python27\lib\site-packages\twisted\internet\tcp.py", line 718, in doRead
    protocol = self.factory.buildProtocol(self._buildAddr(addr))
  File "C:\Python27\lib\site-packages\twisted\internet\protocol.py", line 104, in buildProtocol
    p = self.protocol()
exceptions.TypeError: 'NoneType' object is not callable

If anyone knows the solution kindly help me. I'm just new to twisted .

Was it helpful?

Solution

class ChatServer(Protocol):
    def connectionMade(self):
        print("A Client Has Connected")

factory = Factory()
reactor.listenTCP(80,factory)

You haven't made any association between factory and ChatServer in this code. Try inserting this line:

factory.protocol = ChatServer

In an upcoming (not yet released) version of Twisted, Factory is getting a new class method to make this set-up even easier. Using that version, this example would be even shorter:

class ChatServer(Protocol):
    def connectionMade(self):
        print("A Client Has Connected")

reactor.listenTCP(80, Factory.forProtocol(ChatServer))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top