<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function , with_statement <EOL> import errno <EOL> import os <EOL> import socket <EOL> from tornado . log import app_log <EOL> from tornado . ioloop import IOLoop <EOL> from tornado . iostream import IOStream , SSLIOStream <EOL> from tornado . netutil import bind_sockets , add_accept_handler , ssl_wrap_socket <EOL> from tornado import process <EOL> from tornado . util import errno_from_exception <EOL> try : <EOL> import ssl <EOL> except ImportError : <EOL> ssl = None <EOL> class TCPServer ( object ) : <EOL> r"""<STR_LIT>""" <EOL> def __init__ ( self , io_loop = None , ssl_options = None , max_buffer_size = None , <EOL> read_chunk_size = None ) : <EOL> self . io_loop = io_loop <EOL> self . ssl_options = ssl_options <EOL> self . _sockets = { } <EOL> self . _pending_sockets = [ ] <EOL> self . _started = False <EOL> self . max_buffer_size = max_buffer_size <EOL> self . read_chunk_size = None <EOL> if self . ssl_options is not None and isinstance ( self . ssl_options , dict ) : <EOL> if '<STR_LIT>' not in self . ssl_options : <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> if not os . path . exists ( self . ssl_options [ '<STR_LIT>' ] ) : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> self . ssl_options [ '<STR_LIT>' ] ) <EOL> if ( '<STR_LIT>' in self . ssl_options and <EOL> not os . path . exists ( self . ssl_options [ '<STR_LIT>' ] ) ) : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> self . ssl_options [ '<STR_LIT>' ] ) <EOL> def listen ( self , port , address = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> sockets = bind_sockets ( port , address = address ) <EOL> self . add_sockets ( sockets ) <EOL> def add_sockets ( self , sockets ) : <EOL> """<STR_LIT>""" <EOL> if self . io_loop is None : <EOL> self . io_loop = IOLoop . current ( ) <EOL> for sock in sockets : <EOL> self . _sockets [ sock . fileno ( ) ] = sock <EOL> add_accept_handler ( sock , self . _handle_connection , <EOL> io_loop = self . io_loop ) <EOL> def add_socket ( self , socket ) : <EOL> """<STR_LIT>""" <EOL> self . add_sockets ( [ socket ] ) <EOL> def bind ( self , port , address = None , family = socket . AF_UNSPEC , backlog = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> sockets = bind_sockets ( port , address = address , family = family , <EOL> backlog = backlog ) <EOL> if self . _started : <EOL> self . add_sockets ( sockets ) <EOL> else : <EOL> self . _pending_sockets . extend ( sockets ) <EOL> def start ( self , num_processes = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> assert not self . _started <EOL> self . _started = True <EOL> if num_processes != <NUM_LIT:1> : <EOL> process . fork_processes ( num_processes ) <EOL> sockets = self . _pending_sockets <EOL> self . _pending_sockets = [ ] <EOL> self . add_sockets ( sockets ) <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> for fd , sock in self . _sockets . items ( ) : <EOL> self . io_loop . remove_handler ( fd ) <EOL> sock . close ( ) <EOL> def handle_stream ( self , stream , address ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def _handle_connection ( self , connection , address ) : <EOL> if self . ssl_options is not None : <EOL> assert ssl , "<STR_LIT>" <EOL> try : <EOL> connection = ssl_wrap_socket ( connection , <EOL> self . ssl_options , <EOL> server_side = True , <EOL> do_handshake_on_connect = False ) <EOL> except ssl . SSLError as err : <EOL> if err . args [ <NUM_LIT:0> ] == ssl . SSL_ERROR_EOF : <EOL> return connection . close ( ) <EOL> else : <EOL> raise <EOL> except socket . error as err : <EOL> if errno_from_exception ( err ) in ( errno . ECONNABORTED , errno . EINVAL ) : <EOL> return connection . close ( ) <EOL> else : <EOL> raise <EOL> try : <EOL> if self . ssl_options is not None : <EOL> stream = SSLIOStream ( connection , io_loop = self . io_loop , <EOL> max_buffer_size = self . max_buffer_size , <EOL> read_chunk_size = self . read_chunk_size ) <EOL> else : <EOL> stream = IOStream ( connection , io_loop = self . io_loop , <EOL> max_buffer_size = self . max_buffer_size , <EOL> read_chunk_size = self . read_chunk_size ) <EOL> self . handle_stream ( stream , address ) <EOL> except Exception : <EOL> app_log . error ( "<STR_LIT>" , exc_info = True ) </s>
