Problem using SocketServer.ThreadingTCPServer on Windows?

xtian at hyperactive.co.nz xtian at hyperactive.co.nz
Wed May 30 18:55:01 EDT 2001


Hi -

I'm trying to implement a simple multi-threaded chat-type server,
following an implementation example from Java, but I was amazed to see
that SocketServer had done all the infrastructure work for me. 
I've started with a simple echo server - I can get this working with
straight TCPServer, but when I change the type to ThreadingTCPServer,
I get a very weird error.

I get the error on Win98 and Win2000, both using Python 2.1.

(I'll post the code, because it's so simple.)



##chat.py
import SocketServer

class Handler(SocketServer.BaseRequestHandler):
    def handle(self):
        conn = self.request
        print conn 		# <------ I put these in for debugging
        while 1:
            response = conn.recv(1024)

            if not response:
                break

            print conn, response        # <---------- 
            conn.send(response + '\n')

def go():
    return SocketServer.ThreadingTCPServer(("", 1111), Handler)


The output I get is this:

>>> import chat
>>> server = chat.go()
>>> server.handle_request()
<socket._socketobject instance at 007CE11C>
>>> <socket._socketobject instance at 007CE11C> hi
Exception in thread Thread-1:
Traceback (most recent call last):
  File "d:\python21\lib\threading.py", line 378, in __bootstrap
    self.run()
  File "d:\python21\lib\threading.py", line 366, in run
    apply(self.__target, self.__args, self.__kwargs)
  File "d:\python21\lib\SocketServer.py", line 246, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "d:\python21\lib\SocketServer.py", line 495, in __init__
    self.handle()
  File "d:\python21\lib\play\chat.py", line 14, in handle
    conn.send(response + '\n')
  File "<string>", line 1, in send
AttributeError: 'int' object has no attribute 'send'

It seems that after the second print, for no (obvious to me) reason,
the conn object (which has reported it is a socket twice) is suddenly
an int.

What am I missing?

Thanks
Xtian



More information about the Python-list mailing list