#!/usr/bin/env python import select import socket import sys host = '' #Address of this machine port = 50000 #TCP port to listen on size = 4096 #Maximum size of backlog = 30 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Creating an IPv4, TCP socket server.bind((host,port)) #Bind the socket to this host and port server.listen(backlog) #Can listen to upto 30 connections input = [server] #List of inputs to run 'select' on while 1: inputready,outputready,exceptready = select.select(input,[],[]) #Block until input is ready on any socket for s in inputready: if s == server: # handle the server socket client, address = server.accept() #Accept the client connection print "" input.append(client) #Add the client socket to the input sockets list else: try: # handle all other sockets data = s.recv(size) if data: print "Client " + str(input.index(s)) + ": " + data except: print "Oops, Client %s had an exception!" % str(input.index(s)) server.close()