Connects the socket to a remote address without blocking. May only be called if the socket passed to the constructor was not previously connected. The address parameter is in the same format as for `socket.connect <socket.socket.connect>` for the type of socket pass
(
self: _IOStreamType, address: Any, server_hostname: Optional[str] = None
)
| 1125 | del data |
| 1126 | |
| 1127 | def connect( |
| 1128 | self: _IOStreamType, address: Any, server_hostname: Optional[str] = None |
| 1129 | ) -> "Future[_IOStreamType]": |
| 1130 | """Connects the socket to a remote address without blocking. |
| 1131 | |
| 1132 | May only be called if the socket passed to the constructor was |
| 1133 | not previously connected. The address parameter is in the |
| 1134 | same format as for `socket.connect <socket.socket.connect>` for |
| 1135 | the type of socket passed to the IOStream constructor, |
| 1136 | e.g. an ``(ip, port)`` tuple. Hostnames are accepted here, |
| 1137 | but will be resolved synchronously and block the IOLoop. |
| 1138 | If you have a hostname instead of an IP address, the `.TCPClient` |
| 1139 | class is recommended instead of calling this method directly. |
| 1140 | `.TCPClient` will do asynchronous DNS resolution and handle |
| 1141 | both IPv4 and IPv6. |
| 1142 | |
| 1143 | If ``callback`` is specified, it will be called with no |
| 1144 | arguments when the connection is completed; if not this method |
| 1145 | returns a `.Future` (whose result after a successful |
| 1146 | connection will be the stream itself). |
| 1147 | |
| 1148 | In SSL mode, the ``server_hostname`` parameter will be used |
| 1149 | for certificate validation (unless disabled in the |
| 1150 | ``ssl_options``) and SNI. |
| 1151 | |
| 1152 | Note that it is safe to call `IOStream.write |
| 1153 | <BaseIOStream.write>` while the connection is pending, in |
| 1154 | which case the data will be written as soon as the connection |
| 1155 | is ready. Calling `IOStream` read methods before the socket is |
| 1156 | connected works on some platforms but is non-portable. |
| 1157 | |
| 1158 | .. versionchanged:: 4.0 |
| 1159 | If no callback is given, returns a `.Future`. |
| 1160 | |
| 1161 | .. versionchanged:: 4.2 |
| 1162 | SSL certificates are validated by default; pass |
| 1163 | ``ssl_options=dict(cert_reqs=ssl.CERT_NONE)`` or a |
| 1164 | suitably-configured `ssl.SSLContext` to the |
| 1165 | `SSLIOStream` constructor to disable. |
| 1166 | |
| 1167 | .. versionchanged:: 6.0 |
| 1168 | |
| 1169 | The ``callback`` argument was removed. Use the returned |
| 1170 | `.Future` instead. |
| 1171 | |
| 1172 | """ |
| 1173 | self._connecting = True |
| 1174 | future = Future() # type: Future[_IOStreamType] |
| 1175 | self._connect_future = typing.cast("Future[IOStream]", future) |
| 1176 | try: |
| 1177 | self.socket.connect(address) |
| 1178 | except BlockingIOError: |
| 1179 | # In non-blocking mode we expect connect() to raise an |
| 1180 | # exception with EINPROGRESS or EWOULDBLOCK. |
| 1181 | pass |
| 1182 | except OSError as e: |
| 1183 | # On freebsd, other errors such as ECONNREFUSED may be |
| 1184 | # returned immediately when attempting to connect to |