Connect to the given host and port. Asynchronously returns an `.IOStream` (or `.SSLIOStream` if ``ssl_options`` is not None). Using the ``source_ip`` kwarg, one can specify the source IP address to use when establishing the connection. In case the user needs
(
self,
host: str,
port: int,
af: socket.AddressFamily = socket.AF_UNSPEC,
ssl_options: Optional[Union[Dict[str, Any], ssl.SSLContext]] = None,
max_buffer_size: Optional[int] = None,
source_ip: Optional[str] = None,
source_port: Optional[int] = None,
timeout: Optional[Union[float, datetime.timedelta]] = None,
)
| 215 | self.resolver.close() |
| 216 | |
| 217 | async def connect( |
| 218 | self, |
| 219 | host: str, |
| 220 | port: int, |
| 221 | af: socket.AddressFamily = socket.AF_UNSPEC, |
| 222 | ssl_options: Optional[Union[Dict[str, Any], ssl.SSLContext]] = None, |
| 223 | max_buffer_size: Optional[int] = None, |
| 224 | source_ip: Optional[str] = None, |
| 225 | source_port: Optional[int] = None, |
| 226 | timeout: Optional[Union[float, datetime.timedelta]] = None, |
| 227 | ) -> IOStream: |
| 228 | """Connect to the given host and port. |
| 229 | |
| 230 | Asynchronously returns an `.IOStream` (or `.SSLIOStream` if |
| 231 | ``ssl_options`` is not None). |
| 232 | |
| 233 | Using the ``source_ip`` kwarg, one can specify the source |
| 234 | IP address to use when establishing the connection. |
| 235 | In case the user needs to resolve and |
| 236 | use a specific interface, it has to be handled outside |
| 237 | of Tornado as this depends very much on the platform. |
| 238 | |
| 239 | Raises `TimeoutError` if the input future does not complete before |
| 240 | ``timeout``, which may be specified in any form allowed by |
| 241 | `.IOLoop.add_timeout` (i.e. a `datetime.timedelta` or an absolute time |
| 242 | relative to `.IOLoop.time`) |
| 243 | |
| 244 | Similarly, when the user requires a certain source port, it can |
| 245 | be specified using the ``source_port`` arg. |
| 246 | |
| 247 | .. versionchanged:: 4.5 |
| 248 | Added the ``source_ip`` and ``source_port`` arguments. |
| 249 | |
| 250 | .. versionchanged:: 5.0 |
| 251 | Added the ``timeout`` argument. |
| 252 | """ |
| 253 | if timeout is not None: |
| 254 | if isinstance(timeout, numbers.Real): |
| 255 | timeout = IOLoop.current().time() + timeout |
| 256 | elif isinstance(timeout, datetime.timedelta): |
| 257 | timeout = IOLoop.current().time() + timeout.total_seconds() |
| 258 | else: |
| 259 | raise TypeError("Unsupported timeout %r" % timeout) |
| 260 | if timeout is not None: |
| 261 | addrinfo = await gen.with_timeout( |
| 262 | timeout, self.resolver.resolve(host, port, af) |
| 263 | ) |
| 264 | else: |
| 265 | addrinfo = await self.resolver.resolve(host, port, af) |
| 266 | connector = _Connector( |
| 267 | addrinfo, |
| 268 | functools.partial( |
| 269 | self._create_stream, |
| 270 | max_buffer_size, |
| 271 | source_ip=source_ip, |
| 272 | source_port=source_port, |
| 273 | ), |
| 274 | ) |
no test coverage detected