<s> """<STR_LIT>""" <EOL> import socket <EOL> import ssl <EOL> import weakref <EOL> from pants . _channel import _Channel , HAS_IPV6 , sock_type <EOL> from pants . stream import Stream <EOL> import logging <EOL> log = logging . getLogger ( "<STR_LIT>" ) <EOL> class Server ( _Channel ) : <EOL> """<STR_LIT>""" <EOL> ConnectionClass = Stream <EOL> def __init__ ( self , ConnectionClass = None , ** kwargs ) : <EOL> sock = kwargs . get ( "<STR_LIT>" , None ) <EOL> if sock and sock_type ( sock ) != socket . SOCK_STREAM : <EOL> raise TypeError ( "<STR_LIT>" <EOL> % self . __class__ . __name__ ) <EOL> _Channel . __init__ ( self , ** kwargs ) <EOL> self . _remote_address = None <EOL> self . _local_address = None <EOL> self . _slave = None <EOL> self . listening = False <EOL> self . ssl_enabled = False <EOL> self . _ssl_options = None <EOL> if kwargs . get ( "<STR_LIT>" , None ) is not None : <EOL> self . startSSL ( kwargs [ "<STR_LIT>" ] ) <EOL> if ConnectionClass is not None : <EOL> self . ConnectionClass = ConnectionClass <EOL> self . channels = weakref . WeakValueDictionary ( ) <EOL> @ property <EOL> def remote_address ( self ) : <EOL> """<STR_LIT:U+0020>""" <EOL> return self . _remote_address or self . _socket . getpeername ( ) <EOL> @ remote_address . setter <EOL> def remote_address ( self , val ) : <EOL> self . _remote_address = val <EOL> @ property <EOL> def local_address ( self ) : <EOL> """<STR_LIT:U+0020>""" <EOL> return self . _local_address or self . _socket . getsockname ( ) <EOL> @ local_address . setter <EOL> def local_address ( self , val ) : <EOL> self . _local_address = val <EOL> def startSSL ( self , ssl_options = { } ) : <EOL> """<STR_LIT>""" <EOL> if self . ssl_enabled : <EOL> raise RuntimeError ( "<STR_LIT>" % self ) <EOL> if self . _closed : <EOL> raise RuntimeError ( "<STR_LIT>" % self ) <EOL> if ssl_options . setdefault ( "<STR_LIT>" , True ) is not True : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if ssl_options . setdefault ( "<STR_LIT>" , False ) is not False : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . ssl_enabled = True <EOL> self . _ssl_options = ssl_options <EOL> return self <EOL> def listen ( self , address , backlog = <NUM_LIT> , slave = True ) : <EOL> """<STR_LIT>""" <EOL> if self . listening : <EOL> raise RuntimeError ( "<STR_LIT>" % self ) <EOL> if self . _closed : <EOL> raise RuntimeError ( "<STR_LIT>" % self ) <EOL> address , family , resolved = self . _format_address ( address ) <EOL> if not family : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % repr ( address ) ) <EOL> self . _do_listen ( address , family , backlog , slave ) <EOL> return self <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _closed : <EOL> return <EOL> self . listening = False <EOL> self . ssl_enabled = False <EOL> if self . _slave : <EOL> self . _slave . close ( ) <EOL> self . _safely_call ( self . on_close ) <EOL> self . _remote_address = None <EOL> self . _local_address = None <EOL> _Channel . close ( self ) <EOL> def on_accept ( self , socket , addr ) : <EOL> """<STR_LIT>""" <EOL> connection = self . ConnectionClass ( engine = self . engine , socket = socket ) <EOL> connection . server = self <EOL> self . channels [ connection . fileno ] = connection <EOL> connection . _handle_connect_event ( ) <EOL> def on_close ( self ) : <EOL> """<STR_LIT>""" <EOL> for channel in self . channels . values ( ) : <EOL> channel . close ( flush = False ) <EOL> def on_ssl_wrap_error ( self , sock , addr , exception ) : <EOL> """<STR_LIT>""" <EOL> log . exception ( exception ) <EOL> try : <EOL> sock . close ( ) <EOL> except socket . error : <EOL> pass <EOL> def _do_listen ( self , addr , family , backlog , slave ) : <EOL> """<STR_LIT>""" <EOL> if self . _socket : <EOL> if self . _socket . family != family : <EOL> self . engine . remove_channel ( self ) <EOL> self . _socket_close ( ) <EOL> self . _closed = False <EOL> sock = socket . socket ( family , socket . SOCK_STREAM ) <EOL> self . _socket_set ( sock ) <EOL> self . engine . add_channel ( self ) <EOL> try : <EOL> self . _socket . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> self . _socket . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEPORT , <NUM_LIT:1> ) <EOL> except AttributeError : <EOL> pass <EOL> if hasattr ( socket , "<STR_LIT>" ) and hasattr ( socket , "<STR_LIT>" ) and family == socket . AF_INET6 : <EOL> self . _socket . setsockopt ( socket . IPPROTO_IPV6 , socket . IPV6_V6ONLY , <NUM_LIT:0> ) <EOL> slave = False <EOL> try : <EOL> self . _socket_bind ( addr ) <EOL> self . _socket_listen ( backlog ) <EOL> except socket . error as err : <EOL> self . close ( ) <EOL> raise <EOL> self . listening = True <EOL> self . _safely_call ( self . on_listen ) <EOL> if slave and not isinstance ( addr , str ) and addr [ <NUM_LIT:0> ] == '<STR_LIT>' and HAS_IPV6 : <EOL> try : <EOL> self . _slave = _SlaveServer ( self . engine , self , addr , backlog ) <EOL> except Exception : <EOL> self . _slave = None <EOL> def _handle_read_event ( self ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> try : <EOL> sock , addr = self . _socket_accept ( ) <EOL> except socket . error : <EOL> log . exception ( "<STR_LIT>" % self ) <EOL> try : <EOL> sock . close ( ) <EOL> except socket . error : <EOL> pass <EOL> return <EOL> if sock is None : <EOL> return <EOL> if self . ssl_enabled : <EOL> try : <EOL> sock . setblocking ( False ) <EOL> sock = ssl . wrap_socket ( sock , ** self . _ssl_options ) <EOL> except ssl . SSLError as e : <EOL> self . _safely_call ( self . on_ssl_wrap_error , sock , addr , e ) <EOL> continue <EOL> self . _safely_call ( self . on_accept , sock , addr ) <EOL> def _handle_write_event ( self ) : <EOL> """<STR_LIT>""" <EOL> log . warning ( "<STR_LIT>" % self ) <EOL> class _SlaveServer ( Server ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , engine , server , addr , backlog ) : <EOL> Server . __init__ ( self , engine = engine ) <EOL> self . server = server <EOL> if server . _socket . family == socket . AF_INET6 : <EOL> family = socket . AF_INET <EOL> else : <EOL> family = socket . AF_INET6 <EOL> sock = socket . socket ( family , socket . SOCK_STREAM ) <EOL> self . _socket_set ( sock ) <EOL> self . engine . add_channel ( self ) <EOL> try : <EOL> self . _socket . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> self . _socket . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEPORT , <NUM_LIT:1> ) <EOL> except AttributeError : <EOL> pass <EOL> try : <EOL> self . _socket_bind ( addr ) <EOL> self . _socket_listen ( backlog ) <EOL> except socket . error as err : <EOL> self . close ( ) <EOL> raise <EOL> self . _remote_address = None <EOL> self . _local_address = None <EOL> self . listening = True <EOL> self . on_accept = self . server . on_accept <EOL> def on_close ( self ) : <EOL> if self . server . _slave == self : <EOL> self . server . _slave = None </s>
