<s> from __future__ import print_function <EOL> from socket import socket , AF_INET , SOCK_STREAM , SOCK_DGRAM , SOL_SOCKET , SO_REUSEADDR <EOL> from threading import Thread <EOL> from select import select <EOL> from time import sleep <EOL> from sys import exit <EOL> listening = False <EOL> clisock = None <EOL> done = False <EOL> try : <EOL> input = raw_input <EOL> except NameError : <EOL> pass <EOL> def listen ( port = <NUM_LIT> ) : <EOL> global clisock , listening , done <EOL> s = socket ( AF_INET , SOCK_STREAM ) <EOL> s . setsockopt ( SOL_SOCKET , SO_REUSEADDR , <NUM_LIT:1> ) <EOL> s . bind ( ( '<STR_LIT>' , port ) ) <EOL> s . listen ( <NUM_LIT:3> ) <EOL> listening = True <EOL> sock , addr = s . accept ( ) <EOL> clisock = sock <EOL> print ( "<STR_LIT>" . format ( addr ) ) <EOL> data = "<STR_LIT>" <EOL> while listening : <EOL> try : <EOL> rr , _ , _ = select ( [ sock , ] , [ ] , [ ] , <NUM_LIT:1> ) <EOL> if rr : <EOL> data = sock . recv ( <NUM_LIT> ) <EOL> print ( "<STR_LIT:{}>" . format ( data ) , end = "<STR_LIT>" ) <EOL> except : <EOL> exit ( ) <EOL> print ( "<STR_LIT>" ) <EOL> done = True <EOL> def write ( ) : <EOL> global clisock , listening , done <EOL> while True : <EOL> if clisock : <EOL> data = input ( ) <EOL> if data . strip ( ) . lower ( ) in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> clisock . close ( ) <EOL> exit ( ) <EOL> listening = False <EOL> while not done : sleep ( <NUM_LIT:0.1> ) <EOL> break <EOL> _ , wr , _ = select ( [ ] , [ clisock , ] , [ ] , <NUM_LIT:1> ) <EOL> if wr : <EOL> clisock . sendall ( data + "<STR_LIT:\n>" ) <EOL> else : <EOL> pass <EOL> def nc ( PORT = <NUM_LIT> ) : <EOL> listenThread = Thread ( target = listen , args = ( int ( PORT ) , ) ) <EOL> writeThread = Thread ( target = write ) <EOL> listenThread . start ( ) <EOL> writeThread . start ( ) </s>
