""" server.py see http://docs.python.org/howto/sockets.html and http://docs.python.org/library/socket.html Jim Mahoney | Fall 2012 | Marlboro College """ import socket host = socket.gethostname() port = 10003 connection_queue_size = 5 quit_after_n_connections = 2 buffer_size = 1024 server_socket = socket.socket() server_socket.bind((host, port)) server_socket.listen(connection_queue_size) print "server is listening at %s " % str((host, port)) print "" n_connection = 0 while True: n_connection += 1 (client_socket, client_address) = server_socket.accept() print " connection %i from %s " % (n_connection, str(client_address)) while True: data = client_socket.recv(buffer_size) if not data: break reply = "client sent '%s' " % str(data) print " " + reply client_socket.sendall(reply) print " closing connection" print "" client_socket.close() if n_connection >= quit_after_n_connections: break print "done - saw %i connections" % quit_after_n_connections server_socket.close