<s> from twisted . application import internet , service <EOL> from twisted . internet . protocol import ServerFactory , Protocol <EOL> from twisted . python import log <EOL> class PoetryProtocol ( Protocol ) : <EOL> def connectionMade ( self ) : <EOL> poem = self . factory . service . poem <EOL> log . msg ( '<STR_LIT>' <EOL> % ( len ( poem ) , self . transport . getPeer ( ) ) ) <EOL> self . transport . write ( poem ) <EOL> self . transport . loseConnection ( ) <EOL> class PoetryFactory ( ServerFactory ) : <EOL> protocol = PoetryProtocol <EOL> def __init__ ( self , service ) : <EOL> self . service = service <EOL> class PoetryService ( service . Service ) : <EOL> def __init__ ( self , poetry_file ) : <EOL> self . poetry_file = poetry_file <EOL> def startService ( self ) : <EOL> service . Service . startService ( self ) <EOL> self . poem = open ( self . poetry_file ) . read ( ) <EOL> log . msg ( '<STR_LIT>' % ( self . poetry_file , ) ) <EOL> port = <NUM_LIT> <EOL> iface = '<STR_LIT:localhost>' <EOL> poetry_file = '<STR_LIT>' <EOL> top_service = service . MultiService ( ) <EOL> poetry_service = PoetryService ( poetry_file ) <EOL> poetry_service . setServiceParent ( top_service ) <EOL> factory = PoetryFactory ( poetry_service ) <EOL> tcp_service = internet . TCPServer ( port , factory , interface = iface ) <EOL> tcp_service . setServiceParent ( top_service ) <EOL> application = service . Application ( "<STR_LIT>" ) <EOL> top_service . setServiceParent ( application ) </s>
