[Tutor] Problem with ThreadingTCPServer
Willi Richert
w.richert@gmx.net
Sat, 21 Jul 2001 11:20:18 +0200
This is a multi-part message in MIME format.
------=_NextPart_000_0086_01C111D7.1F1FC0A0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi,
since 2 days I am debugging the Python socket stuff.
First I had a simple following:
class GameHandler(SocketServer.StreamRequestHandler): =20
def handle(self):
request=3Dself.rfile.readline(512)
# do interesting stuff
result=3D"blabla"
self.wfile.write(result) =20
server=3DSocketServer.TCPServer((HOST, PORT), GameHandler)
server.serve_forever()
and everything worked fine as long as only one client was accessing the =
server.
Then I changed to=20
...
server=3DSocketServer.ThreadingTCPServer((HOST, PORT), GameHandler)
...
and I get now:
server=3DSocketServer.TCPServer((HOST, PORT), GameHandler)
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 247, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "d:\python21\lib\SocketServer.py", line 497, in __init__
self.handle()
File "D:\Hearts\Hearts\gameserver.py", line 223, in handle
request=3Dself.rfile.readline(512)
File "d:\python21\lib\socket.py", line 238, in readline
new =3D self._sock.recv(self._rbufsize)
AttributeError: 'int' object has no attribute 'recv'
If I am right the socket in _fileobject.readline is 0.
But why? Only because of the multithreading?
Does anybody have an idea?
Thanks very much for every hint,
willi
------=_NextPart_000_0086_01C111D7.1F1FC0A0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2614.3500" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hi,</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>since 2 days I am debugging the Python =
socket=20
stuff.</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>First I had a simple =
following:</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>class=20
GameHandler(SocketServer.StreamRequestHandler): =20
<BR> def handle(self):</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =20
request=3Dself.rfile.readline(512)</FONT></DIV>
<DIV><FONT face=3DArial =
size=3D2> # do=20
interesting stuff</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> =20
result=3D"blabla"</FONT></DIV>
<DIV><FONT face=3DArial =
size=3D2> =20
self.wfile.write(result) </FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>server=3DSocketServer.TCPServer((HOST, =
PORT),=20
GameHandler)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>server.serve_forever()</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>and everything worked fine as long as =
only one=20
client was accessing the server.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Then I changed to </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>...</FONT></DIV>
<DIV><FONT face=3DArial =
size=3D2>server=3DSocketServer.ThreadingTCPServer((HOST,=20
PORT), GameHandler)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>and I get now:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>server=3DSocketServer.TCPServer((HOST, =
PORT),=20
GameHandler)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Exception in thread =
Thread-1:<BR>Traceback (most=20
recent call last):<BR> File "d:\python21\lib\threading.py", line =
378, in=20
__bootstrap<BR> self.run()<BR> File=20
"d:\python21\lib\threading.py", line 366, in run<BR> =20
apply(self.__target, self.__args, self.__kwargs)<BR> File=20
"d:\python21\lib\SocketServer.py", line 247, in=20
finish_request<BR> self.RequestHandlerClass(request,=20
client_address, self)<BR> File "d:\python21\lib\SocketServer.py", =
line=20
497, in __init__<BR> self.handle()<BR> File=20
"D:\Hearts\Hearts\gameserver.py", line 223, in =
handle<BR> =20
request=3Dself.rfile.readline(512)<BR> File =
"d:\python21\lib\socket.py",=20
line 238, in readline<BR> new =3D=20
self._sock.recv(self._rbufsize)<BR>AttributeError: 'int' object has no =
attribute=20
'recv'<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>If I am right the socket in =
_fileobject.readline is=20
0.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>But why? Only because of the=20
multithreading?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Does anybody have an idea?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Thanks very much for every =
hint,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>willi<BR></DIV></FONT></BODY></HTML>
------=_NextPart_000_0086_01C111D7.1F1FC0A0--