<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function , with_statement <EOL> import errno <EOL> import os <EOL> import platform <EOL> import socket <EOL> import stat <EOL> from tornado . concurrent import dummy_executor , run_on_executor <EOL> from tornado . ioloop import IOLoop <EOL> from tornado . platform . auto import set_close_exec <EOL> from tornado . util import u , Configurable , errno_from_exception <EOL> try : <EOL> import ssl <EOL> except ImportError : <EOL> ssl = None <EOL> try : <EOL> xrange <EOL> except NameError : <EOL> xrange = range <EOL> if hasattr ( ssl , '<STR_LIT>' ) and hasattr ( ssl , '<STR_LIT>' ) : <EOL> ssl_match_hostname = ssl . match_hostname <EOL> SSLCertificateError = ssl . CertificateError <EOL> elif ssl is None : <EOL> ssl_match_hostname = SSLCertificateError = None <EOL> else : <EOL> import backports . ssl_match_hostname <EOL> ssl_match_hostname = backports . ssl_match_hostname . match_hostname <EOL> SSLCertificateError = backports . ssl_match_hostname . CertificateError <EOL> u ( '<STR_LIT:foo>' ) . encode ( '<STR_LIT>' ) <EOL> _ERRNO_WOULDBLOCK = ( errno . EWOULDBLOCK , errno . EAGAIN ) <EOL> if hasattr ( errno , "<STR_LIT>" ) : <EOL> _ERRNO_WOULDBLOCK += ( errno . WSAEWOULDBLOCK , ) <EOL> _DEFAULT_BACKLOG = <NUM_LIT> <EOL> def bind_sockets ( port , address = None , family = socket . AF_UNSPEC , <EOL> backlog = _DEFAULT_BACKLOG , flags = None ) : <EOL> """<STR_LIT>""" <EOL> sockets = [ ] <EOL> if address == "<STR_LIT>" : <EOL> address = None <EOL> if not socket . has_ipv6 and family == socket . AF_UNSPEC : <EOL> family = socket . AF_INET <EOL> if flags is None : <EOL> flags = socket . AI_PASSIVE <EOL> bound_port = None <EOL> for res in set ( socket . getaddrinfo ( address , port , family , socket . SOCK_STREAM , <EOL> <NUM_LIT:0> , flags ) ) : <EOL> af , socktype , proto , canonname , sockaddr = res <EOL> if ( platform . system ( ) == '<STR_LIT>' and address == '<STR_LIT:localhost>' and <EOL> af == socket . AF_INET6 and sockaddr [ <NUM_LIT:3> ] != <NUM_LIT:0> ) : <EOL> continue <EOL> try : <EOL> sock = socket . socket ( af , socktype , proto ) <EOL> except socket . error as e : <EOL> if errno_from_exception ( e ) == errno . EAFNOSUPPORT : <EOL> continue <EOL> raise <EOL> set_close_exec ( sock . fileno ( ) ) <EOL> if os . name != '<STR_LIT>' : <EOL> sock . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> if af == socket . AF_INET6 : <EOL> if hasattr ( socket , "<STR_LIT>" ) : <EOL> sock . setsockopt ( socket . IPPROTO_IPV6 , socket . IPV6_V6ONLY , <NUM_LIT:1> ) <EOL> host , requested_port = sockaddr [ : <NUM_LIT:2> ] <EOL> if requested_port == <NUM_LIT:0> and bound_port is not None : <EOL> sockaddr = tuple ( [ host , bound_port ] + list ( sockaddr [ <NUM_LIT:2> : ] ) ) <EOL> sock . setblocking ( <NUM_LIT:0> ) <EOL> sock . bind ( sockaddr ) <EOL> bound_port = sock . getsockname ( ) [ <NUM_LIT:1> ] <EOL> sock . listen ( backlog ) <EOL> sockets . append ( sock ) <EOL> return sockets <EOL> if hasattr ( socket , '<STR_LIT>' ) : <EOL> def bind_unix_socket ( file , mode = <NUM_LIT> , backlog = _DEFAULT_BACKLOG ) : <EOL> """<STR_LIT>""" <EOL> sock = socket . socket ( socket . AF_UNIX , socket . SOCK_STREAM ) <EOL> set_close_exec ( sock . fileno ( ) ) <EOL> sock . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> sock . setblocking ( <NUM_LIT:0> ) <EOL> try : <EOL> st = os . stat ( file ) <EOL> except OSError as err : <EOL> if errno_from_exception ( err ) != errno . ENOENT : <EOL> raise <EOL> else : <EOL> if stat . S_ISSOCK ( st . st_mode ) : <EOL> os . remove ( file ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" , file ) <EOL> sock . bind ( file ) <EOL> os . chmod ( file , mode ) <EOL> sock . listen ( backlog ) <EOL> return sock <EOL> def add_accept_handler ( sock , callback , io_loop = None ) : <EOL> """<STR_LIT>""" <EOL> if io_loop is None : <EOL> io_loop = IOLoop . current ( ) <EOL> def accept_handler ( fd , events ) : <EOL> for i in xrange ( _DEFAULT_BACKLOG ) : <EOL> try : <EOL> connection , address = sock . accept ( ) <EOL> except socket . error as e : <EOL> if errno_from_exception ( e ) in _ERRNO_WOULDBLOCK : <EOL> return <EOL> if errno_from_exception ( e ) == errno . ECONNABORTED : <EOL> continue <EOL> raise <EOL> callback ( connection , address ) <EOL> io_loop . add_handler ( sock , accept_handler , IOLoop . READ ) <EOL> def is_valid_ip ( ip ) : <EOL> """<STR_LIT>""" <EOL> if not ip or '<STR_LIT:\x00>' in ip : <EOL> return False <EOL> try : <EOL> res = socket . getaddrinfo ( ip , <NUM_LIT:0> , socket . AF_UNSPEC , <EOL> socket . SOCK_STREAM , <EOL> <NUM_LIT:0> , socket . AI_NUMERICHOST ) <EOL> return bool ( res ) <EOL> except socket . gaierror as e : <EOL> if e . args [ <NUM_LIT:0> ] == socket . EAI_NONAME : <EOL> return False <EOL> raise <EOL> return True <EOL> class Resolver ( Configurable ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def configurable_base ( cls ) : <EOL> return Resolver <EOL> @ classmethod <EOL> def configurable_default ( cls ) : <EOL> return BlockingResolver <EOL> def resolve ( self , host , port , family = socket . AF_UNSPEC , callback = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class ExecutorResolver ( Resolver ) : <EOL> """<STR_LIT>""" <EOL> def initialize ( self , io_loop = None , executor = None , close_executor = True ) : <EOL> self . io_loop = io_loop or IOLoop . current ( ) <EOL> if executor is not None : <EOL> self . executor = executor <EOL> self . close_executor = close_executor <EOL> else : <EOL> self . executor = dummy_executor <EOL> self . close_executor = False <EOL> def close ( self ) : <EOL> if self . close_executor : <EOL> self . executor . shutdown ( ) <EOL> self . executor = None <EOL> @ run_on_executor <EOL> def resolve ( self , host , port , family = socket . AF_UNSPEC ) : <EOL> addrinfo = socket . getaddrinfo ( host , port , family , socket . SOCK_STREAM ) <EOL> results = [ ] <EOL> for family , socktype , proto , canonname , address in addrinfo : <EOL> results . append ( ( family , address ) ) <EOL> return results <EOL> class BlockingResolver ( ExecutorResolver ) : <EOL> """<STR_LIT>""" <EOL> def initialize ( self , io_loop = None ) : <EOL> super ( BlockingResolver , self ) . initialize ( io_loop = io_loop ) <EOL> class ThreadedResolver ( ExecutorResolver ) : <EOL> """<STR_LIT>""" <EOL> _threadpool = None <EOL> _threadpool_pid = None <EOL> def initialize ( self , io_loop = None , num_threads = <NUM_LIT:10> ) : <EOL> threadpool = ThreadedResolver . _create_threadpool ( num_threads ) <EOL> super ( ThreadedResolver , self ) . initialize ( <EOL> io_loop = io_loop , executor = threadpool , close_executor = False ) <EOL> @ classmethod <EOL> def _create_threadpool ( cls , num_threads ) : <EOL> pid = os . getpid ( ) <EOL> if cls . _threadpool_pid != pid : <EOL> cls . _threadpool = None <EOL> if cls . _threadpool is None : <EOL> from concurrent . futures import ThreadPoolExecutor <EOL> cls . _threadpool = ThreadPoolExecutor ( num_threads ) <EOL> cls . _threadpool_pid = pid <EOL> return cls . _threadpool <EOL> class OverrideResolver ( Resolver ) : <EOL> """<STR_LIT>""" <EOL> def initialize ( self , resolver , mapping ) : <EOL> self . resolver = resolver <EOL> self . mapping = mapping <EOL> def close ( self ) : <EOL> self . resolver . close ( ) <EOL> def resolve ( self , host , port , * args , ** kwargs ) : <EOL> if ( host , port ) in self . mapping : <EOL> host , port = self . mapping [ ( host , port ) ] <EOL> elif host in self . mapping : <EOL> host = self . mapping [ host ] <EOL> return self . resolver . resolve ( host , port , * args , ** kwargs ) <EOL> _SSL_CONTEXT_KEYWORDS = frozenset ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def ssl_options_to_context ( ssl_options ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( ssl_options , dict ) : <EOL> assert all ( k in _SSL_CONTEXT_KEYWORDS for k in ssl_options ) , ssl_options <EOL> if ( not hasattr ( ssl , '<STR_LIT>' ) or <EOL> isinstance ( ssl_options , ssl . SSLContext ) ) : <EOL> return ssl_options <EOL> context = ssl . SSLContext ( <EOL> ssl_options . get ( '<STR_LIT>' , ssl . PROTOCOL_SSLv23 ) ) <EOL> if '<STR_LIT>' in ssl_options : <EOL> context . load_cert_chain ( ssl_options [ '<STR_LIT>' ] , ssl_options . get ( '<STR_LIT>' , None ) ) <EOL> if '<STR_LIT>' in ssl_options : <EOL> context . verify_mode = ssl_options [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in ssl_options : <EOL> context . load_verify_locations ( ssl_options [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in ssl_options : <EOL> context . set_ciphers ( ssl_options [ '<STR_LIT>' ] ) <EOL> if hasattr ( ssl , '<STR_LIT>' ) : <EOL> context . options |= ssl . OP_NO_COMPRESSION <EOL> return context <EOL> def ssl_wrap_socket ( socket , ssl_options , server_hostname = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> context = ssl_options_to_context ( ssl_options ) <EOL> if hasattr ( ssl , '<STR_LIT>' ) and isinstance ( context , ssl . SSLContext ) : <EOL> if server_hostname is not None and getattr ( ssl , '<STR_LIT>' ) : <EOL> return context . wrap_socket ( socket , server_hostname = server_hostname , <EOL> ** kwargs ) <EOL> else : <EOL> return context . wrap_socket ( socket , ** kwargs ) <EOL> else : <EOL> return ssl . wrap_socket ( socket , ** dict ( context , ** kwargs ) ) </s>
