def get_socket(conf):
 try:
  bind_port = int(conf['bind_port'])
 except (ValueError, KeyError, TypeError):
  raise ConfigFilePortError()
 bind_addr = (conf.get('bind_ip', '0.0.0.0'), bind_port)
 address_family = [addr[0] for addr in socket.getaddrinfo(bind_addr[0], bind_addr[1], socket.AF_UNSPEC, socket.SOCK_STREAM) if (addr[0] in (socket.AF_INET, socket.AF_INET6))][0]
 sock = None
 bind_timeout = int(conf.get('bind_timeout', 30))
 retry_until = (time.time() + bind_timeout)
 warn_ssl = False
 while ((not sock) and (time.time() < retry_until)):
  try:
   sock = listen(bind_addr, backlog=int(conf.get('backlog', 4096)), family=address_family)
   if ('cert_file' in conf):
    warn_ssl = True
    sock = ssl.wrap_socket(sock, certfile=conf['cert_file'], keyfile=conf['key_file'])
  except socket.error as err:
   if (err.args[0] != errno.EADDRINUSE):
    raise
   sleep(0.1)
 if (not sock):
  raise Exception((_('Could not bind to %(addr)s:%(port)s after trying for %(timeout)s seconds') % {'addr': bind_addr[0], 'port': bind_addr[1], 'timeout': bind_timeout}))
 sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
 sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
 if hasattr(socket, 'TCP_KEEPIDLE'):
  sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 600)
 if warn_ssl:
  ssl_warning_message = _('WARNING: SSL should only be enabled for testing purposes. Use external SSL termination for a production deployment.')
  get_logger(conf).warning(ssl_warning_message)
  print(ssl_warning_message)
 return sock

def get_socket ( conf ) : try : bind_port = int ( conf [ 'bind_port' ] ) except ( ValueError , KeyError , TypeError ) : raise ConfigFilePortError ( ) bind_addr = ( conf . get ( 'bind_ip' , '0.0.0.0' ) , bind_port ) address_family = [ addr [ 0 ] for addr in socket . getaddrinfo ( bind_addr [ 0 ] , bind_addr [ 1 ] , socket . AF_UNSPEC , socket . SOCK_STREAM ) if ( addr [ 0 ] in ( socket . AF_INET , socket . AF_INET6 ) ) ] [ 0 ] sock = None bind_timeout = int ( conf . get ( 'bind_timeout' , 30 ) ) retry_until = ( time . time ( ) + bind_timeout ) warn_ssl = False while ( ( not sock ) and ( time . time ( ) < retry_until ) ) : try : sock = listen ( bind_addr , backlog = int ( conf . get ( 'backlog' , 4096 ) ) , family = address_family ) if ( 'cert_file' in conf ) : warn_ssl = True sock = ssl . wrap_socket ( sock , certfile = conf [ 'cert_file' ] , keyfile = conf [ 'key_file' ] ) except socket . error as err : if ( err . args [ 0 ] != errno . EADDRINUSE ) : raise sleep ( 0.1 ) if ( not sock ) : raise Exception ( ( _ ( 'Could not bind to %(addr)s:%(port)s after trying for %(timeout)s seconds' ) % { 'addr' : bind_addr [ 0 ] , 'port' : bind_addr [ 1 ] , 'timeout' : bind_timeout } ) ) sock . setsockopt ( socket . SOL_SOCKET , socket . SO_KEEPALIVE , 1 ) sock . setsockopt ( socket . IPPROTO_TCP , socket . TCP_NODELAY , 1 ) if hasattr ( socket , 'TCP_KEEPIDLE' ) : sock . setsockopt ( socket . IPPROTO_TCP , socket . TCP_KEEPIDLE , 600 ) if warn_ssl : ssl_warning_message = _ ( 'WARNING: SSL should only be enabled for testing purposes. Use external SSL termination for a production deployment.' ) get_logger ( conf ) . warning ( ssl_warning_message ) print ( ssl_warning_message ) return sock
return a socket object for the addr ip-version agnostic .