Handling more than one request on a socket

John Roth johnroth at ameritech.net
Wed Aug 29 00:11:19 EDT 2001


"Jeff Grimmett" <grimmtoothtoo at yahoo.com> wrote in message
news:2df3d89d.0108281844.7736d5b9 at posting.google.com...
> Hi, pythonistas,
>
> I'm trying to come to grips with the issue of dealing with sockets,
> or, more precisely, a socket that can accept and process 'n' number of
> requests on a particular address.
>
> Right now I'm going the route of opening an inet streaming socket,
> binding it to the port, and listen()ing for requests. For example, for
> a telnet port:
>
>    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>    s.bind(('', 23))
>    s.listen(1)

Doesn't your listen have to be in a loop? Each time it returns, the return
from listen is a new connection socket descriptor (file descriptor). Then
you
spin off whatever you need to do to handle the connection - a new
process, thread or whatever.

> ... then ... ?  I've tried a number of approaches, but so far all I
> can get to work is a single connection at a time, and in most cases
> it's good for one recv().

If you only do the listen once, then you're going to get exactly that
behavior. Once you turn a socket into a listener, you can't do normal
socket I/O on it. All it's good for is the listen function.


> This is an issue I'm going to be looking at in a lot of cases, so I'd
> like to iron it out now if I can. Any suggestions on where to start?

Unix Network Programming, by the late W. Richard Stevens. The
concepts are identical for Windows.

John Roth





More information about the Python-list mailing list