473,499 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5103
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4848
by: vishal | last post by:
Hi I a am executing a stored procedure from C code using SQLExecute . It takes some time to Execute the procedure. However when I see the processor usage during that time it shows 100% cpu usage....
0
230
by: Deasun | last post by:
I just delivered a windows client .net app and have noticed on client Pc, and for that matter on dev PC, the CPU in the Task manager is pinged at 100%. Yet the PC acts fine. You can launch IE and...
1
3769
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket...
1
1488
by: Mike Dole | last post by:
I'm sorry to bother you with this question but it was either this or giving up and trying to go for a simpler solution (which I will if this is not gonna work out..) I'm afraid this is way out...
7
2391
by: Ed McNierney | last post by:
I'm trying to use ServerXMLHTTP on an ASP (not ASP.NET) page to retrieve large binary data from a remote server. When the request is large (more than a few megabytes), the ServerXMLHTTP page jumps...
5
1619
by: fniles | last post by:
I am having problem with thread. I have a Session class with public string variable (called Message) that I set from my Main program. In the session class it checks for the value of Message...
2
2444
by: jld | last post by:
Hi, I developed an asp.net based eCommerce Website for a client and it is hosted at discount asp. The site is quite interactive, queries a database a lot and uses ajax.asp.net to spice up...
0
1711
by: khu84 | last post by:
Here is client server very simple code, seems to work with telnet but with with web client code gives blank output. Following is the server code:- <?php function...
7
2561
by: Vishal | last post by:
Hi, I am writing a CGI to serve files to the caller. I was wondering if there is any way to tell in my CGI if the client browser is still connected. If it is not, i want to execute some special...
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.