468,549 Members | 2,042 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,549 developers. It's quick & easy.

One more socket programming question

I'm now experimenting with the SocketServer class. Originally I
subclassed the StreamRequestHandler to make my own custom handler, but a
result of this seems to be that the client socket closes after it has
been used, instead of staying open.

Just as a test, I decided to use BaseRequestHandler instead, because I
know its methods aren't implemented. So this is what I have:

-----
import SocketServer

host = ''
port = 51234
address = (host, port)
buffer_size = 1024

class MyRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
print '...connected from:', self.client_address
data = self.request.recv(buffer_size)
self.request.send('%s %s' % ('You typed:', data))

socket_server = SocketServer.TCPServer(address, MyRequestHandler)
print 'waiting for connection...'
socket_server.serve_forever()
------

------
from socket import *

host = 'localhost'
port = 51234
address = (host, port)
buffer_size = 1024

client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect(address)

while True:
data = raw_input('')
if not data:
break
client_socket.send(data)
data = client_socket.recv(buffer_size)
print data

client_socket.close()
------

But this only seems to work one time, and then subsequent attempts
return nothing, and then the client program seems to crash (or just
close on its own).

What's happening here?
Jun 27 '08 #1
2 876
John Salerno <jo******@gmailNOSPAM.comwrote:
>
I'm now experimenting with the SocketServer class. Originally I
subclassed the StreamRequestHandler to make my own custom handler, but a
result of this seems to be that the client socket closes after it has
been used, instead of staying open.
Right. "handle" is not called for one REQUEST at a time, it's called for
one CONNECTION at a time. If you need a connection to be persistent, then
your handle() function needs to sit in a loop making recv calls until you
detect that the conversation is complete.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 27 '08 #2
"Tim Roberts" <ti**@probo.comwrote in message
news:sj********************************@4ax.com...
John Salerno <jo******@gmailNOSPAM.comwrote:
>>
I'm now experimenting with the SocketServer class. Originally I
subclassed the StreamRequestHandler to make my own custom handler, but a
result of this seems to be that the client socket closes after it has
been used, instead of staying open.

Right. "handle" is not called for one REQUEST at a time, it's called for
one CONNECTION at a time. If you need a connection to be persistent, then
your handle() function needs to sit in a loop making recv calls until you
detect that the conversation is complete.
Ah, so I need to rewrite my handle method. I was thinking that the
specialized setup() and/or finish() calls from StreamRequestHandler were the
reason that the connection was closed after one use (which is why I tried
BaseRequestHandler instead).

Thanks.
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jean-Philippe Guyon | last post: by
2 posts views Thread by dream machine | last post: by
1 post views Thread by John Sheppard | last post: by
5 posts views Thread by Justin Creasy | last post: by
2 posts views Thread by djc | last post: by
14 posts views Thread by =?Utf-8?B?TWlrZVo=?= | last post: by
3 posts views Thread by Stuart | last post: by
1 post views Thread by =?Utf-8?B?UmFqbmk=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.