MCPcopy
hub / github.com/tornadoweb/tornado / connect

Method connect

tornado/tcpclient.py:221–295  ·  view source on GitHub ↗

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,
    )

Source from the content-addressed store, hash-verified

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
260 elif isinstance(timeout, datetime.timedelta):
261 timeout = IOLoop.current().time() + timeout.total_seconds()
262 else:
263 raise TypeError("Unsupported timeout %r" % timeout)
264 if timeout is not None:
265 addrinfo = await gen.with_timeout(
266 timeout, self.resolver.resolve(host, port, af)
267 )
268 else:
269 addrinfo = await self.resolver.resolve(host, port, af)
270 connector = _Connector(
271 addrinfo,
272 functools.partial(
273 self._create_stream,
274 max_buffer_size,
275 source_ip=source_ip,
276 source_port=source_port,
277 ),
278 )

Callers 1

try_connectMethod · 0.45

Calls 6

_ConnectorClass · 0.85
timeMethod · 0.80
currentMethod · 0.80
start_tlsMethod · 0.80
resolveMethod · 0.45
startMethod · 0.45

Tested by

no test coverage detected