MCPcopy Index your code
hub / github.com/RustPython/RustPython / create_connection

Function create_connection

Lib/socket.py:828–876  ·  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 *timeo

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

Source from the content-addressed store, hash-verified

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

Callers 1

get_server_certificateFunction · 0.90

Calls 10

lenFunction · 0.85
getaddrinfoFunction · 0.70
socketClass · 0.70
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