Python Unhandled Error Traceback (most recent call last):
<p>This is my code, I was looking for answers but none resolve my problem. 
I'm new to Twisted and still learning.</p>

<p>Why do I get this error after I run <code>telnet localhost 72</code> in the Windows command prompt? </p>

<p>This is my code:</p>

<pre><code>from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor


class chatprotocol(LineReceiver):
    def __init__(self, factory):
        self.factory = factory
        self.name = None
        self.state = "REGiSTER"

    def connectionMade(self):
        self.sendLine("What's your Name?")

    def connectionLost(self, reason):
        if self.name in self.factory.users:
            del self.factory.Users[self.name]
            self.broadcastMessage("%s has left channel." % (self.name,))

    def lineReceived(self, line):
        if self.state == "REGISTER":
            self.handle_REGISTER(line)
        else:
            self.handle_CHAT(line)

    def handle_REGISTER(self, name):
        if name in self.factory.users:
            self.sendLine("Name taken , please choose another")
            return
        self.sendline("welcome ,%s!" % (name,))
        self.broadcastMessage("%s has joined the channel." % (name,))
        self.name = name
        self.factory.users[name] = self
        self.state = "CHAT"

    def handle_CHAT(self, message):
        message = "&lt;%s&gt; %s" % (self.name, message)
        self.broadcastMessage(message)

    def broadcastMessage(self, message):
        for name, protocol in self.factory.users.iteritems():
            if protocol != self:
                protocol.sendLine(message)

class ChatFactory(Factory):
    def __init__(self):
        self.users = {}

    def buildProtocol(self, addr):
        return chatprotocol(self)


reactor.listenTCP(72, ChatFactory())
reactor.run()
</code></pre>

<p>the error message:</p>

<pre><code>Unhandled Error
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\twisted\python\log.py", line 84, 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 149, in _doReadOrWrite
    why = getattr(selectable, method)()
--- &lt;exception caught here&gt; ---
  File "C:\Python27\lib\site-packages\twisted\internet\tcp.py", line 1066, in doRead
    protocol = self.factory.buildProtocol(self._buildAddr(addr))
  File "C:\Python27\lib\site-packages\twisted\internet\protocol.py", line 131, in buildProtocol
    p = self.protocol()
exceptions.TypeError: 'NoneType' object is not callable
</code></pre>

<p>@firman Are you sure you're giving us the EXACT code that's causing issues? The code above (if it were written properly) cannot possibly give that error. The error occurs in the <code>buildProtocol</code> function of the <code>Factory</code> class, which you overloaded in your example. I've edited your question and corrected some syntax errors. Try it again and see if it works. If not then post your code that's giving you errors.</p>

<p>The error you got is caused when you don't set the <code>protocol</code> variable in your factory object and it remains <code>None</code>. For example, let's comment out the <code>buildProtocol</code> function in your factory class so that it looks like this:</p>

<pre><code>class ChatFactory(Factory):
    def __init__(self):
        self.users = {}

#    def buildProtocol(self, addr):
#        return chatprotocol(self)
</code></pre>

<p>I get the error you originally got:</p>

<pre><code>--- &lt;exception caught here&gt; ---
  File "/Projects/virtenv/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 1066, in doRead
    protocol = self.factory.buildProtocol(self._buildAddr(addr))
  File "/Projects/virtenv/local/lib/python2.7/site-packages/twisted/internet/protocol.py", line 131, in buildProtocol
    p = self.protocol()
exceptions.TypeError: 'NoneType' object is not callable
</code></pre>

<p>To fix this issue, you can do 1 of 2 things. You can overload <code>buildProtocol()</code> to return the protocol object you want (you did this in your original example). Or you can set the factory object to a variable, then set the <code>protocol</code> class variable. So your main method should look like:</p>

<pre><code>factory = ChatFactory()
factory.protocol = chatprotocol
reactor.listenTCP(72, factory)
</code></pre>

python twisted