httpx

HttpxSession represents a wrapper-class around ClientSession from httpx

Currently HttpxSession is a default session used in litegram.Bot

class litegram.client.session.httpx.HttpxSession(proxy: str | None = None, **kwargs: Any)[source]

Usage example

from litegram import Bot
from litegram.client.session.httpx import HttpxSession

session = HttpxSession()
bot = Bot('42:token', session=session)

Proxy requests in HttpxSession

In order to use HttpxSession with proxy connector you have to install httpx-socks

Binding session to bot:

from litegram import Bot
from litegram.client.session.httpx import HttpxSession

session = HttpxSession(proxy="protocol://host:port/")
bot = Bot(token="bot token", session=session)

Note

Only following protocols are supported: http(tunneling), socks4(a), socks5 as httpx_socks documentation claims.

Authorization

Proxy authorization credentials can be specified in proxy URL or come as an instance of httpx.BasicAuth containing login and password.

Consider examples:

from httpx import BasicAuth
from litegram.client.session.httpx import HttpxSession

auth = BasicAuth(login="user", password="password")
session = HttpxSession(proxy=("protocol://host:port", auth))

or simply include your basic auth credential in URL

session = HttpxSession(proxy="protocol://user:password@host:port")

Note

Litegram prefers BasicAuth over username and password in URL, so if proxy URL contains login and password and BasicAuth object is passed at the same time litegram will use login and password from BasicAuth instance.

Proxy chains

Since httpx-socks supports proxy chains, you’re able to use them in litegram

Example of chain proxies:

from httpx import BasicAuth
from litegram.client.session.httpx import HttpxSession

auth = BasicAuth(login="user", password="password")
session = HttpxSession(
    proxy={
        "protocol0://host0:port0",
        "protocol1://user:password@host1:port1",
        ("protocol2://host2:port2", auth),
    }  # can be any iterable if not set
)