A non-blocking TCP connection factory. .. versionchanged:: 5.0 The ``io_loop`` argument (deprecated since version 4.1) has been removed.
| 200 | |
| 201 | |
| 202 | class TCPClient: |
| 203 | """A non-blocking TCP connection factory. |
| 204 | |
| 205 | .. versionchanged:: 5.0 |
| 206 | The ``io_loop`` argument (deprecated since version 4.1) has been removed. |
| 207 | """ |
| 208 | |
| 209 | def __init__(self, resolver: Optional[Resolver] = None) -> None: |
| 210 | if resolver is not None: |
| 211 | self.resolver = resolver |
| 212 | self._own_resolver = False |
| 213 | else: |
| 214 | self.resolver = Resolver() |
| 215 | self._own_resolver = True |
| 216 | |
| 217 | def close(self) -> None: |
| 218 | if self._own_resolver: |
| 219 | self.resolver.close() |
| 220 | |
| 221 | async def connect( |
| 222 | self, |
| 223 | host: str, |
| 224 | port: int, |
| 225 | af: socket.AddressFamily = socket.AF_UNSPEC, |
| 226 | ssl_options: Optional[Union[Dict[str, Any], ssl.SSLContext]] = None, |
| 227 | max_buffer_size: Optional[int] = None, |
| 228 | source_ip: Optional[str] = None, |
| 229 | source_port: Optional[int] = None, |
| 230 | timeout: Optional[Union[float, datetime.timedelta]] = None, |
| 231 | ) -> IOStream: |
| 232 | """Connect to the given host and port. |
| 233 | |
| 234 | Asynchronously returns an `.IOStream` (or `.SSLIOStream` if |
| 235 | ``ssl_options`` is not None). |
| 236 | |
| 237 | Using the ``source_ip`` kwarg, one can specify the source |
| 238 | IP address to use when establishing the connection. |
| 239 | In case the user needs to resolve and |
| 240 | use a specific interface, it has to be handled outside |
| 241 | of Tornado as this depends very much on the platform. |
| 242 | |
| 243 | Raises `TimeoutError` if the input future does not complete before |
| 244 | ``timeout``, which may be specified in any form allowed by |
| 245 | `.IOLoop.add_timeout` (i.e. a `datetime.timedelta` or an absolute time |
| 246 | relative to `.IOLoop.time`) |
| 247 | |
| 248 | Similarly, when the user requires a certain source port, it can |
| 249 | be specified using the ``source_port`` arg. |
| 250 | |
| 251 | .. versionchanged:: 4.5 |
| 252 | Added the ``source_ip`` and ``source_port`` arguments. |
| 253 | |
| 254 | .. versionchanged:: 5.0 |
| 255 | Added the ``timeout`` argument. |
| 256 | """ |
| 257 | if timeout is not None: |
| 258 | if isinstance(timeout, numbers.Real): |
| 259 | timeout = IOLoop.current().time() + timeout |
no outgoing calls