import socket import time ############################################################ ## ## TCP Server Socket - By Fischetti P. ## ############################################################ ## ## Serve un solo client. ## Per servire un altro client occorre riavviare ## il programma. ## ############################################################ TCP_PORT = 6000 TCP_IP = '127.0.0.1' ##usare socket.gethostname() in modo che il socket sia visibile ##al mondo esterno. ##Con serversocket.bind(('', TCP_PORT)) ##o serversocket.bind(('localhost', TCP_PORT)) ##o serversocket.bind(('127.0.0.1', TCP_PORT)) ##il socket e' visibile solo all'interno della stessa macchina. TCP_IP=socket.gethostname() BUFFER_SIZE = 1024 sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sk.bind((TCP_IP, TCP_PORT)) sk.listen(1) print 'Waiting connection on ',socket.gethostname(),TCP_PORT conn, addr = sk.accept() print 'Connection address:', addr while 1: data = conn.recv(BUFFER_SIZE) if not data: break print "received data:", data conn.send(data) # echo conn.close()