############################################################ ## ## TCP Server Socket - By Fischetti P. ## ############################################################ ## ## Serve + client ## ma il client deve inviare la string 'quit' ## per chiudere la connessione e permettere ## ad altri di continuare. ## ############################################################ import socket import time def strTohex(s): lst = [] for ch in s: hv = hex(ord(ch)) lst.append(hv) return lst TCP_IP = '127.0.0.1' TCP_PORT = 6000 BUFFER_SIZE = 1024 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #s.bind((TCP_IP, TCP_PORT)) s.bind((socket.gethostname(), TCP_PORT)) s.listen(1) buff=[] while 1: print 'Waiting connection on ',socket.gethostname(),TCP_PORT conn, addr = s.accept() print 'Connection address:', addr bContinue=True while bContinue: data = conn.recv(BUFFER_SIZE) if not data: break print "received data:", data conn.send(data) # echo ##TELNET send string: if data=='quix': bContinue = False print 'Connection address:', addr, ' Closed' ## ## #TELNET char ## if len(data)==2 and data[0]=='\r' and data[1]=='\n': ## if ''.join(buff).lower()=='quit': ## bContinue=False ## print 'Connection address:', addr, ' Closed' ## buff=[] ## else: ## buff.append(data) conn.close()