Getting unhandled error and connection get lost when a client tries to communicate with chat server in twisted

Posted by user2433888 on Stack Overflow See other posts from Stack Overflow or by user2433888
Published on 2013-06-29T16:16:33Z Indexed on 2013/06/29 16:21 UTC
Read the original article Hit count: 232

Filed under:
|
from twisted.internet.protocol import Protocol,Factory
from twisted.internet import reactor

class ChatServer(Protocol):
def connectionMade(self):
    print "A Client Has Connected"
    self.factory.clients.append(self)
    print"clients are ",self.factory.clients
    self.transport.write('Hello,Welcome to the telnet chat to sign in type aim:YOUR NAME HERE to send a messsage type msg:YOURMESSAGE '+'\n')

def connectionLost(self,reason):
    self.factory.clients.remove(self)
    self.transport.write('Somebody was disconnected from the server')

def dataReceived(self,data):
    #print "data is",data
    a = data.split(':')
    if len(a) > 1:
        command = a[0]
    content = a[1]

    msg=""
    if command =="iam":
    self.name + "has joined"

    elif command == "msg":
    ma=sg = self.name + ":" +content

    print msg
    for c in self.factory.clients:
        c.message(msg)
def message(self,message):
    self.transport.write(message + '\n')

factory = Factory()
factory.protocol = ChatServer
factory.clients = []
reactor.listenTCP(80,factory)
print "Iphone Chat server started"
reactor.run()

The above code is running succesfully...but when i connect the client (by typing telnet localhost 80) to this chatserver and try to write message ,connection gets lost and following errors occurs :

Iphone Chat server started
A Client Has Connected
clients are [<__main__.ChatServer instance at 0x024AC0A8>]
Unhandled Error
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\twisted\python\log.py", line 84, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
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)()
File "C:\Python27\lib\site-packages\twisted\internet\tcp.py", line 199, in doRead
rval = self.protocol.dataReceived(data)
File "D:\chatserverultimate.py", line 21, in dataReceived
content = a[1]
exceptions.IndexError: list index out of range

Where am I going wrong?

© Stack Overflow or respective owner

Related posts about twisted

Related posts about twisted.client