#!/soft/python-2.4-bin/python import os, sys, time, socket, threading def main(): ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #socket level options ssock.bind( ('', 5001) ) ssock.listen(20) print "Hello.." n = 0 while 1: try: cx, addr = ssock.accept() n += 1 server = threading.Thread(target=process, name='client%d'%n, args=[cx, n] ) server.start() except: continue def process(cx, n): flo = cx.makefile('r', 0) print '%s opening connexion'% threading.currentThread().getName() counter = threading.Thread(target=moron, name='moron%d'%n, args=[cx]) counter.start() while 1: try: line = flo.readline() print '%s: %s'%( threading.currentThread().getName(), line ), cx.sendall( line ) except: print '%s closing connexion'% threading.currentThread().getName() cx.close() return def moron(cx): count = 0 while 1: try: time.sleep(1) count += 1 cx.sendall( '%d\r\n'%count ) except: return main()