[Tutor] AttributeError - ChatServer
Bob Hinkle
hinkle.bob at gmail.com
Fri Jan 27 02:42:23 CET 2006
While following a tutorial on how to create a simple chat server I stumbled
upon this problem:
AttributeError: ChatServer instance has no attribute 'decriptors'
here is my code:
#...................................................................................
import socket
import select
class ChatServer:
def __init__(self,port):
self.port = port;
self.srvsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.srvsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.srvsock.bind(("", port))
self.srvsock.listen(5)
self.descriptors = [self.srvsock]
print 'Chat Server started on port %s' % port
def run ( self ):
while 1:
#Await even on a readable socket descriptor
(sread, swrite, sexc) = select.select(self.descriptors, [], [])
#Iterate through the tagged read descriptors
for sock in sread:
#check if received a connect to the server
if sock == self.srvsock:
self.accept_new_connection()
else:
# received something on a client socket
str = sock.recv(100)
# check and see if the peer closed socket
if str == '':
host,port = sock.getpeername()
str = 'Client left %s:%s\r\n' % (host, port)
self.broadcast_string(str,sock)
sock.close
self.descriptors.remose(sock)
else:
host,port = sock.getpeername()
newstr = '[%s:%s] %s' % (host, port, str)
self.broadcast_string(newstr,sock)
def broadcast_string(self, str, omit_sock):
for sock in self.decriptors:
if sock != self.srvsock and sock != omit_sock:
sock.send(str)
print str,
def accept_new_connection( self ):
newsock, (remhost, remport) = self.srvsock.accept()
self.descriptors.append( newsock )
newsock.send("You're connected to Chat Server!\r\n")
str = "Client joined %s:%s\r\n" % (remhost, remport)
self.broadcast_string( str, newsock )
myserver = ChatServer( 23000 )
myserver.run()
#.......................................................................
It starts fine but when someone connects, I get the error.
[saiph]$ python telnetserver2.py
Chat Server started on port 23000
Traceback (most recent call last):
File "telnetserver2.py", line 74, in ?
myserver.run()
File "telnetserver2.py", line 32, in run
self.accept_new_connection()
File "telnetserver2.py", line 69, in accept_new_connection
self.broadcast_string( str, newsock )
File "telnetserver2.py", line 53, in broadcast_string
for sock in self.decriptors:
AttributeError: ChatServer instance has no attribute 'decriptors'
Any help would be appreciated! TIA
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060126/2210db64/attachment.htm
More information about the Tutor
mailing list