Open a streaming transport connection to a given address specified by
host
and
port
.
套接字族可以是
AF_INET
or
AF_INET6
从属
host
(或
系列
自变量,若有提供)。
套接字类型将是
SOCK_STREAM
.
protocol_factory
must be a callable returning an
asyncio protocol
实现。
This method will try to establish the connection in the background. When successful, it returns a
(transport, protocol)
对。
The chronological synopsis of the underlying operation is as follows:
-
The connection is established and a
transport
is created for it.
-
protocol_factory
is called without arguments and is expected to return a
protocol
实例。
-
The protocol instance is coupled with the transport by calling its
connection_made()
方法。
-
A
(transport, protocol)
tuple is returned on success.
The created transport is an implementation-dependent bidirectional stream.
其它自变量:
-
ssl
: if given and not false, a SSL/TLS transport is created (by default a plain TCP transport is created). If
ssl
是
ssl.SSLContext
object, this context is used to create the transport; if
ssl
is
True
, a default context returned from
ssl.create_default_context()
被使用。
-
server_hostname
sets or overrides the hostname that the target server’s certificate will be matched against. Should only be passed if
ssl
不是
None
. By default the value of the
host
argument is used. If
host
is empty, there is no default and you must pass a value for
server_hostname
。若
server_hostname
is an empty string, hostname matching is disabled (which is a serious security risk, allowing for potential man-in-the-middle attacks).
-
系列
,
proto
,
flags
are the optional address family, protocol and flags to be passed through to getaddrinfo() for
host
resolution. If given, these should all be integers from the corresponding
socket
module constants.
-
happy_eyeballs_delay
, if given, enables Happy Eyeballs for this connection. It should be a floating-point number representing the amount of time in seconds to wait for a connection attempt to complete, before starting the next attempt in parallel. This is the “Connection Attempt Delay” as defined in
RFC 8305
. A sensible default value recommended by the RFC is
0.25
(250 milliseconds).
-
interleave
controls address reordering when a host name resolves to multiple IP addresses. If
0
or unspecified, no reordering is done, and addresses are tried in the order returned by
getaddrinfo()
. If a positive integer is specified, the addresses are interleaved by address family, and the given integer is interpreted as “First Address Family Count” as defined in
RFC 8305
。默认为
0
if
happy_eyeballs_delay
is not specified, and
1
if it is.
-
sock
, if given, should be an existing, already connected
socket.socket
object to be used by the transport. If
sock
is given, none of
host
,
port
,
系列
,
proto
,
flags
,
happy_eyeballs_delay
,
interleave
and
local_addr
should be specified.
注意
The
sock
argument transfers ownership of the socket to the transport created. To close the socket, call the transport’s
close()
方法。
-
local_addr
,若给定,是
(local_host, local_port)
tuple used to bind the socket locally. The
local_host
and
local_port
are looked up using
getaddrinfo()
, similarly to
host
and
port
.
-
ssl_handshake_timeout
is (for a TLS connection) the time in seconds to wait for the TLS handshake to complete before aborting the connection.
60.0
seconds if
None
(default).
-
ssl_shutdown_timeout
is the time in seconds to wait for the SSL shutdown to complete before aborting the connection.
30.0
seconds if
None
(default).
-
all_errors
determines what exceptions are raised when a connection cannot be created. By default, only a single
Exception
is raised: the first exception if there is only one or all errors have same message, or a single
OSError
with the error messages combined. When
all_errors
is
True
,
ExceptionGroup
will be raised containing all exceptions (even if there is only one).
3.5 版改变:
Added support for SSL/TLS in
ProactorEventLoop
.
3.6 版改变:
套接字选项
socket.TCP_NODELAY
is set by default for all TCP connections.
3.7 版改变:
添加
ssl_handshake_timeout
参数。
3.8 版改变:
添加
happy_eyeballs_delay
and
interleave
参数。
Happy Eyeballs Algorithm: Success with Dual-Stack Hosts. When a server’s IPv4 path and protocol are working, but the server’s IPv6 path and protocol are not working, a dual-stack client application experiences significant connection delay compared to an IPv4-only client. This is undesirable because it causes the dual-stack client to have a worse user experience. This document specifies requirements for algorithms that reduce this user-visible delay and provides an algorithm.
更多信息:
https://datatracker.ietf.org/doc/html/rfc6555
3.11 版改变:
添加
ssl_shutdown_timeout
参数。
3.12 版改变:
all_errors
被添加。