#! /usr/bin/env python # -*- encoding: utf-8 -*- # file: sock_srv.py # author: O. Dalle import sys,os,socket,time,select connsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) connsock.bind(("",8801)) connsock.listen(5) nb_open = 0 # Create list of potential readers and place connection socket in # first position potential_readers = [connsock] first = True while first or nb_open > 0: first = False readers,writers,errors = select.select(potential_readers,[],[],60) for sock_ready in readers: if sock_ready == connsock: # The return value is a pair (conn, address) where conn is a # new socket object usable to send and receive data on the # connection, and address is the address bound to the socket # on the other end of the connection. conn,(addr,port) = connsock.accept() # Add new connection top potential readers potential_readers.append(conn) print "Incoming connexion from %s on port %d..."%(addr,port) raw_input("Appuyer sur entree...") nb_open+=1 else: msg=sock_ready.recv(1) if (len(msg) == 0): print "NULL message. Closing connction..." sock_ready.close() # Remove the closed connection from potential readers for i in xrange(1,len(potential_readers)): if potential_readers[i] == sock_ready: potential_readers.pop(i) nb_open -= 1 else: os.write(1,msg) connsock.close() print "last connection closed. Bye!" sys.exit(0)