[Tutor] Turning a "script" into an "application"

Bill Mill bill.mill at gmail.com
Wed Oct 27 08:48:11 CEST 2004


Yes, this will only handle one connection at a time. If you want to
write a server to handle a lot of connections concurrently, there are
two main approaches. They are nicely illustrated by two different

Medusa (http://www.amk.ca/python/code/medusa.html) uses a pool of
sockets and iterates through them using something called polling. To
learn more about this, study up on the asynchat module.

Apache (http://httpd.apache.org/) versions 2.0 and above use a
threaded model, where each request to the server creates a new thread
in the backgrounds to handle the request, then goes back to listening.
Study the threading module to learn more about this.

As the python docs will tell you, threading is simpler and more
popular, but for a network server, polling is often more efficient.

Bill Mill
bill.mill at gmail.com

On Wed, 27 Oct 2004 16:18:16 +1000, Joe Healy
<joe at omc-international.com.au> wrote:
> Bill Mill wrote:
> >Joe,
> >
> >Not sure what you mean. In python, sockets send and receive strings.
> >If a server has a socket listening on port X, and it associates some
> >string with some function call, all it has to do is call that
> >function. In pythonish pseudocode:
> >
> >#######
> ># Server
> >######
> >
> >actions = {'func1': func1,   #function reference
> >        'func2': myfunc2     #another
> >        }
> >while 1:
> >    socket.bind((host, port))
> >    socket.listen()
> >    while data to read:
> >        read data into variable buf
> >    actions[buf]()
> >
> >#######
> ># Client
> >######
> >
> >socket.connect((server, port))
> >socket.send(func_to_perform)
> >
> >Is this at all what you mean?
> >
> >
> >
> Yes, I did not realise it was so simple. I gather that this will only
> handle one request at a time and not do anything in the mean time, but
> it looks like a good way of communicating between two processes. I have
> a number of programs collecting data from various places and this looks
> like the beginning of a way to keep an eye on them.
> Thanks,
> Joe Healy

More information about the Tutor mailing list