Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > One more socket programming question

Reply
Thread Tools

One more socket programming question

 
 
John Salerno
Guest
Posts: n/a
 
      06-18-2008
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?
 
Reply With Quote
 
 
 
 
Tim Roberts
Guest
Posts: n/a
 
      06-18-2008
John Salerno <> wrote:
>
>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,
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
 
 
 
John Salerno
Guest
Posts: n/a
 
      06-18-2008
"Tim Roberts" <> wrote in message
news:...
> John Salerno <> wrote:
>>
>>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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 1 02-03-2009 06:20 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM



Advertisments