Need help with socket server code

Steve Holden sholden at holdenweb.com
Mon Jul 16 08:51:29 EDT 2001


"Shane Anglin" <shane.anglin at mtni.net> wrote in message
news:mailman.995132124.31082.python-list at python.org...
> Here's the basics of what I want to do with my new app:
> Main loop is:
> 1 - check socket for any incoming data
> 1a - if no data on socket, go to 2, else get the data and place it into a
list
> 2 - print out list
> 3 - go to 1 and do it all over again
>
> Currently, I can create a TCP socket server app using examples that will
sit and wait (idle 99% of the time) and accepts the incoming data just fine,
my 'print list' part is not processing at all until I get any new data in...
for example, 1-wait on a client connect on socket, when a connection
happens, get data, 2- place new data into list, print list, 3 -go to 1 and
do it all over again...   in this scenario, #1 has the rest of my code
'hostage' until a client connection is made and closed.
>
Shane:

I'm unsure from your description as to whether you want the list to *only*
contain the data from the last submission, or whether you want it to grow as
individual submissions are added. However, your pseudo-code is rather
unstructured.

I presume you must have some reason for not using a structure like:

    while 1:
        l = listfrom(socket)
        print list

where the listfrom() function simply waits for a connection on a socket and
returns data. Do you need to do other computations while the socket code is
running in the background?

You might want to take a look at two framework module which do most of the
work for you, and which are quite easy to use: SocketServer allows you to
build synchronous servers (where nothing happens when there's no work coming
in o ver network sockets), and asyncore builds asynchronous servers, where
interactions can easily happen with several different clients on several
different connections concurrently.

regards
 STeve
--
http://www.holdenweb.com/








More information about the Python-list mailing list