# Released under the MIT License. See LICENSE for details.#"""Networking related functionality."""from__future__importannotationsimportsocketimportthreadingimportipaddressfromtypingimportTYPE_CHECKINGifTYPE_CHECKING:pass
[docs]classNetworkSubsystem:"""Network related app subsystem."""def__init__(self)->None:importbabase._envassertbabase._env._g_net_warm_start_threadisnotNonebabase._env._g_net_warm_start_thread.join()babase._env._g_net_warm_start_thread=Noneassertbabase._env._g_net_warm_start_ssl_contextisnotNoneself.sslcontext=babase._env._g_net_warm_start_ssl_contextbabase._env._g_net_warm_start_ssl_context=Noneassertbabase._env._g_net_warm_start_pool_managerisnotNoneself.urllib3pool=babase._env._g_net_warm_start_pool_managerbabase._env._g_net_warm_start_pool_manager=None# Anyone accessing/modifying zone_pings should hold this lock,# as it is updated by a background thread.self.zone_pings_lock=threading.Lock()# Zone IDs mapped to average pings. This will remain empty# until enough pings have been run to be reasonably certain# that a nearby server has been pinged.self.zone_pings:dict[str,float]={}# For debugging/progress.self.connectivity_state=''self.transport_state=''self.server_time_offset_hours:float|None=None
[docs]defpre_interpreter_shutdown(self)->None:"""Called just before interpreter shuts down."""self.urllib3pool.clear()
[docs]defget_ip_address_type(addr:str)->socket.AddressFamily:"""Return an address-type given an address. Can be :attr:`socket.AF_INET` or :attr:`socket.AF_INET6`. """version=ipaddress.ip_address(addr).versionifversion==4:returnsocket.AF_INETassertversion==6returnsocket.AF_INET6
# Docs-generation hack; import some stuff that we likely only forward-declared# in our actual source code so that docs tools can find it.fromtypingimport(Coroutine,Any,Literal,Callable,Generator,Awaitable,Sequence,Self)importasynciofromconcurrent.futuresimportFuturefrompathlibimportPathfromenumimportEnum