MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / create_connection

Function create_connection

tools/python-3.11.9-amd64/Lib/socket.py:809–857  ·  view source on GitHub ↗

Connect to *address* and return the socket object. Convenience function. Connect to *address* (a 2-tuple ``(host, port)``) and return the socket object. Passing the optional *timeout* parameter will set the timeout on the socket instance before attempting to connect. If no *

(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
                      source_address=None, *, all_errors=False)

Source from the content-addressed store, hash-verified

807_GLOBAL_DEFAULT_TIMEOUT = object()
808
809def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
810 source_address=None, *, all_errors=False):
811 """Connect to *address* and return the socket object.
812
813 Convenience function. Connect to *address* (a 2-tuple ``(host,
814 port)``) and return the socket object. Passing the optional
815 *timeout* parameter will set the timeout on the socket instance
816 before attempting to connect. If no *timeout* is supplied, the
817 global default timeout setting returned by :func:`getdefaulttimeout`
818 is used. If *source_address* is set it must be a tuple of (host, port)
819 for the socket to bind as a source address before making the connection.
820 A host of '' or port 0 tells the OS to use the default. When a connection
821 cannot be created, raises the last error if *all_errors* is False,
822 and an ExceptionGroup of all errors if *all_errors* is True.
823 """
824
825 host, port = address
826 exceptions = []
827 for res in getaddrinfo(host, port, 0, SOCK_STREAM):
828 af, socktype, proto, canonname, sa = res
829 sock = None
830 try:
831 sock = socket(af, socktype, proto)
832 if timeout is not _GLOBAL_DEFAULT_TIMEOUT:
833 sock.settimeout(timeout)
834 if source_address:
835 sock.bind(source_address)
836 sock.connect(sa)
837 # Break explicitly a reference cycle
838 exceptions.clear()
839 return sock
840
841 except error as exc:
842 if not all_errors:
843 exceptions.clear() # raise only the last error
844 exceptions.append(exc)
845 if sock is not None:
846 sock.close()
847
848 if len(exceptions):
849 try:
850 if not all_errors:
851 raise exceptions[0]
852 raise ExceptionGroup("create_connection failed", exceptions)
853 finally:
854 # Break explicitly a reference cycle
855 exceptions.clear()
856 else:
857 raise error("getaddrinfo returns an empty list")
858
859
860def has_dualstack_ipv6():

Callers 1

get_server_certificateFunction · 0.90

Calls 9

getaddrinfoFunction · 0.85
socketClass · 0.85
errorFunction · 0.70
settimeoutMethod · 0.45
bindMethod · 0.45
connectMethod · 0.45
clearMethod · 0.45
appendMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected