server not accepting second connect
Falko Spiller
falkoNOSPAM at ostrakon.de
Tue Oct 15 08:27:05 EDT 2002
hello,
just learning python, and my toy project shall do something like a very
simple chat (or mud). so if somebody connects with a telnet like
applikation, she can chat with other connected people.
i started with code for server and requesthandler from the http server
application, throwing out everything i didnt need.
in the request handler there is this handle() method.
the problem is (or seems to be):
if the handle() method does not return, the server doesnt accept new
connections.
if the handle message returns, the connection seems to get destroyed.
at least i get an error message:
Unhandled exception in thread:
Traceback (most recent call last):
File "server.py", line 68, in run
line = self.fileToServer.readline()
File "C:\Python22\lib\socket.py", line 238, in readline
new = self._sock.recv(self._rbufsize)
AttributeError: 'int' object has no attribute 'recv'
server.py is my file.
currently i have to start a server for every single client that wants to
connect, and, well, knowing this topic from java, i think thats not state of
the art :-)
anybody able to suggest a way? or to provide/point to example-code?
thank you
regards, falko
-----------------------------------------
platform: python 2.2.1, M$ Windos 2000
source fragments:
class Server(SocketServer.TCPServer):
allow_reuse_address = 1 # Seems to make sense in testing environment
def server_bind(self):
"""Override server_bind to store the server name."""
if len(labyrinth)<1:
makeRooms()
SocketServer.TCPServer.server_bind(self)
host, port = self.socket.getsockname()
self.server_name = socket.getfqdn(host)
self.server_port = port
serverName.append(self.server_name)
serverName.append(self.server_port)
class RequestHandler(SocketServer.StreamRequestHandler):
"""request handler base class.
The various request details are stored in instance variables:
- client_address is the client IP address in the form (host,
port);
- rfile is a file object open for reading positioned at the
start of the optional input data part;
- wfile is a file object open for writing.
"""
def handle(self):
print self.client_address, 'connected.'
self.wfile.write("Hello Stranger, welcome to "+serverName[0]+' (port
'+str(serverName[1])+')\n')
self.wfile.write("The first word you utter will be your name.")
player = Player(self, self.rfile,self.wfile)
player.room = labyrinth[0][0]
player.room.players.append(player)
player.name = None# self.client_address[1]
player.run()
# thread.start_new_thread(player.run,())
the two different ways are the two last lines. the player.run(9 anth the
comment, starting the new thread.
the player tries to do a readline at requesthandler.rfile, and that crashes
in the new thread.
More information about the Python-list
mailing list