Source

networking / src / selectecho.py

#!/usr/bin/env python 

""" 
An echo server that uses select to handle multiple clients at a time. 
Entering any line of input at the terminal will exit the server. 
""" 

import logging
import select 
import socket 
import sys 

LOG_FILENAME = 'example.log'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)

host = '' 
port = 50000
backlog = 5 
size = 1024 
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
server.bind((host,port))
server.listen(backlog)
input = [server, sys.stdin]

while True:
    inputready,outputready,exceptready = select.select(input,[],[])

    for s in inputready:
        if s == server:
            # handle the server socket
            client, address = server.accept()
            logging.debug("Client accepted from %s on %d port" % (address[0], address[1]))
            input.append(client) 

        elif s == sys.stdin: 
            # handle standard input 
            junk = sys.stdin.readline() 
            running = 0 

        else: 
            # handle all other sockets 
            data = s.recv(size) 
            if data:
                logging.debug("Message: %s" % data)
                s.send(data)
            else:
                s.close()
                input.remove(s)
server.close()