471,339 Members | 1,281 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,339 software developers and data experts.

100% CPU Usage when a tcp client is disconnected

The following is a code I am using for a simple tcp echo server.
When I run it and then connect to it (with Telnet for example) if I
shout down the telnet the CPU tops 100% of usage and saty there
forever.
Can one tell what am I doing wrong?

#code.py

import SocketServer

class MyServer(SocketServer.BaseRequestHandler ):
def setup(self):
print self.client_address, 'connected!'
self.request.send('hi ' + str(self.client_address) + '\n')

def handle(self):
while 1:
data = self.request.recv(1024)
self.request.send(data)
if data.strip() == 'bye':
return

def finish(self):
print self.client_address, 'disconnected!'
self.request.send('bye ' + str(self.client_address) + '\n')

#server host is a tuple ('host', port)
server = SocketServer.ThreadingTCPServer(('', 50008), MyServer)
server.serve_forever()
Nov 22 '07 #1
5 4940
On Nov 22, 9:53 am, Tzury Bar Yochay <Afro.Syst...@gmail.comwrote:
The following is a code I am using for a simple tcp echo server.
When I run it and then connect to it (with Telnet for example) if I
shout down the telnet the CPU tops 100% of usage and saty there
forever....
def handle(self):
while 1:
data = self.request.recv(1024)
self.request.send(data)
if data.strip() == 'bye':
return
I forget exactly how the superclass works, but
that while 1 looks suspicious. Try chaning it
to

data = "dummy"
while data:
...

-- Aaron Watters

===
http://www.xfeedme.com/nucular/pydis...+infinite+loop
Nov 22 '07 #2
data = "dummy"
while data:
...
Thanks Alot
Nov 22 '07 #3
Tzury Bar Yochay <Af**********@gmail.comwrites:
The following is a code I am using for a simple tcp echo server.
When I run it and then connect to it (with Telnet for example) if I
shout down the telnet the CPU tops 100% of usage and saty there
forever. Can one tell what am I doing wrong?
If you shut down telnet, self.request.recv(1024) returns an empty
string, meaning EOF, and you start inflooping.
def handle(self):
while 1:
data = self.request.recv(1024)
self.request.send(data)
if data.strip() == 'bye': # add: or data == ''
return
Nov 22 '07 #4
Thank Hrvoje as well
Nov 22 '07 #5
Aaron Watters wrote:
On Nov 22, 9:53 am, Tzury Bar Yochay <Afro.Syst...@gmail.comwrote:
>The following is a code I am using for a simple tcp echo server.
When I run it and then connect to it (with Telnet for example) if I
shout down the telnet the CPU tops 100% of usage and saty there
forever....
def handle(self):
while 1:
data = self.request.recv(1024)
self.request.send(data)
if data.strip() == 'bye':
return
... Try changing it to ...
data = "dummy"
while data:
...
Even better:
from functools import partial

def handle(self):
for data in iter(partial(self.request.recv, 1024), ''):
self.request.send(data)
if data.strip() == 'bye':
break
else:
raise ValueError('Gone w/o a "bye"') # or IOError

-Scott

Nov 22 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by vishal | last post: by
reply views Thread by Deasun | last post: by
5 posts views Thread by fniles | last post: by
2 posts views Thread by jld | last post: by
reply views Thread by khu84 | last post: by
7 posts views Thread by Vishal | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.