HTTPXRequest

class telegram.request.HTTPXRequest(connection_pool_size=1, proxy_url=None, read_timeout=5.0, write_timeout=5.0, connect_timeout=5.0, pool_timeout=1.0, http_version='1.1', socket_options=None, proxy=None, media_write_timeout=20.0)[source]

Bases: telegram.request.BaseRequest

Implementation of BaseRequest using the library httpx.

New in version 20.0.

Parameters:
  • connection_pool_size (int, optional) –

    Number of connections to keep in the connection pool. Defaults to 1.

    Note

    Independent of the value, one additional connection will be reserved for telegram.Bot.get_updates().

  • proxy_url (str, optional) –

    Legacy name for proxy, kept for backward compatibility. Defaults to None.

    Deprecated since version 20.7.

  • read_timeout (float | None, optional) – If passed, specifies the maximum amount of time (in seconds) to wait for a response from Telegram’s server. This value is used unless a different value is passed to do_request(). Defaults to 5.

  • write_timeout (float | None, optional) –

    If passed, specifies the maximum amount of time (in seconds) to wait for a write operation to complete (in terms of a network socket; i.e. POSTing a request or uploading a file). This value is used unless a different value is passed to do_request(). Defaults to 5.

    Hint

    This timeout is used for all requests except for those that upload media/files. For the latter, media_write_timeout is used.

  • connect_timeout (float | None, optional) – If passed, specifies the maximum amount of time (in seconds) to wait for a connection attempt to a server to succeed. This value is used unless a different value is passed to do_request(). Defaults to 5.

  • pool_timeout (float | None, optional) –

    If passed, specifies the maximum amount of time (in seconds) to wait for a connection to become available. This value is used unless a different value is passed to do_request(). Defaults to 1.

    Warning

    With a finite pool timeout, you must expect telegram.error.TimedOut exceptions to be thrown when more requests are made simultaneously than there are connections in the connection pool!

  • http_version (str, optional) –

    If "2" or "2.0", HTTP/2 will be used instead of HTTP/1.1. Defaults to "1.1".

    New in version 20.1.

    Changed in version 20.2: Reset the default version to 1.1.

    Changed in version 20.5: Accept "2" as a valid value.

  • socket_options (Collection[tuple], optional) –

    Socket options to be passed to the underlying library.

    Note

    The values accepted by this parameter depend on the operating system. This is a low-level parameter and should only be used if you are familiar with these concepts.

    New in version 20.7.

  • proxy (str | httpx.Proxy | httpx.URL, optional) –

    The URL to a proxy server, a httpx.Proxy object or a httpx.URL object. For example 'http://127.0.0.1:3128' or 'socks5://127.0.0.1:3128'. Defaults to None.

    Note

    • The proxy URL can also be set via the environment variables HTTPS_PROXY or ALL_PROXY. See the docs of httpx for more info.

    • HTTPS proxies can be configured by passing a httpx.Proxy object with a corresponding ssl_context.

    • For Socks5 support, additional dependencies are required. Make sure to install PTB via pip install "python-telegram-bot[socks]" in this case.

    • Socks5 proxies can not be set via environment variables.

    New in version 20.7.

  • media_write_timeout (float | None, optional) –

    Like write_timeout, but used only for requests that upload media/files. This value is used unless a different value is passed to do_request.write_timeout of do_request(). Defaults to 20 seconds.

    New in version 21.0.

async do_request(url, method, request_data=None, read_timeout=None, write_timeout=None, connect_timeout=None, pool_timeout=None)[source]

See BaseRequest.do_request().

property http_version[source]

Used HTTP version, see http_version.

New in version 20.2.

Type:

str

async initialize()[source]

See BaseRequest.initialize().

property read_timeout[source]

See BaseRequest.read_timeout.

Returns:

The default read timeout in seconds as passed to

HTTPXRequest.read_timeout.

Return type:

float | None

async shutdown()[source]

See BaseRequest.shutdown().