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 ` for the type of socket pass
(
self: _IOStreamType, address: Any, server_hostname: Optional[str] = None
)
| 1150 | del data |
| 1151 | |
| 1152 | def connect( |
| 1153 | self: _IOStreamType, address: Any, server_hostname: Optional[str] = None |
| 1154 | ) -> "Future[_IOStreamType]": |
| 1155 | """Connects the socket to a remote address without blocking. |
| 1156 | |
| 1157 | May only be called if the socket passed to the constructor was |
| 1158 | not previously connected. The address parameter is in the |
| 1159 | same format as for `socket.connect <socket.socket.connect>` for |
| 1160 | the type of socket passed to the IOStream constructor, |
| 1161 | e.g. an ``(ip, port)`` tuple. Hostnames are accepted here, |
| 1162 | but will be resolved synchronously and block the IOLoop. |
| 1163 | If you have a hostname instead of an IP address, the `.TCPClient` |
| 1164 | class is recommended instead of calling this method directly. |
| 1165 | `.TCPClient` will do asynchronous DNS resolution and handle |
| 1166 | both IPv4 and IPv6. |
| 1167 | |
| 1168 | If ``callback`` is specified, it will be called with no |
| 1169 | arguments when the connection is completed; if not this method |
| 1170 | returns a `.Future` (whose result after a successful |
| 1171 | connection will be the stream itself). |
| 1172 | |
| 1173 | In SSL mode, the ``server_hostname`` parameter will be used |
| 1174 | for certificate validation (unless disabled in the |
| 1175 | ``ssl_options``) and SNI (if supported; requires Python |
| 1176 | 2.7.9+). |
| 1177 | |
| 1178 | Note that it is safe to call `IOStream.write |
| 1179 | <BaseIOStream.write>` while the connection is pending, in |
| 1180 | which case the data will be written as soon as the connection |
| 1181 | is ready. Calling `IOStream` read methods before the socket is |
| 1182 | connected works on some platforms but is non-portable. |
| 1183 | |
| 1184 | .. versionchanged:: 4.0 |
| 1185 | If no callback is given, returns a `.Future`. |
| 1186 | |
| 1187 | .. versionchanged:: 4.2 |
| 1188 | SSL certificates are validated by default; pass |
| 1189 | ``ssl_options=dict(cert_reqs=ssl.CERT_NONE)`` or a |
| 1190 | suitably-configured `ssl.SSLContext` to the |
| 1191 | `SSLIOStream` constructor to disable. |
| 1192 | |
| 1193 | .. versionchanged:: 6.0 |
| 1194 | |
| 1195 | The ``callback`` argument was removed. Use the returned |
| 1196 | `.Future` instead. |
| 1197 | |
| 1198 | """ |
| 1199 | self._connecting = True |
| 1200 | future = Future() # type: Future[_IOStreamType] |
| 1201 | self._connect_future = typing.cast("Future[IOStream]", future) |
| 1202 | try: |
| 1203 | self.socket.connect(address) |
| 1204 | except BlockingIOError: |
| 1205 | # In non-blocking mode we expect connect() to raise an |
| 1206 | # exception with EINPROGRESS or EWOULDBLOCK. |
| 1207 | pass |
| 1208 | except socket.error as e: |
| 1209 | # On freebsd, other errors such as ECONNREFUSED may be |