-
Notifications
You must be signed in to change notification settings - Fork 44
Description
Module this issue is about
ptbcontrib.aiohttp_request (specifically the AiohttpRequest class)
Steps to reproduce
- Install
python-telegram-bot,ptbcontrib.aiohttp_request, andaiohttp-socks. - Define a SOCKS5 proxy URL with authentication (e.g.,
PROXY_URL = "socks5://user:pass@host:port"). - Create the
AiohttpRequestmotor:request_motor = AiohttpRequest(proxy=PROXY_URL) - Build the application:
application = Application.builder().token(TOKEN).request(request_motor).get_updates_request(request_motor).build() - Try to initialize the application:
await application.initialize()
Expected behaviour
The application should initialize successfully, connecting to the Telegram API through the SOCKS5 proxy.
Ideally, AiohttpRequest would either:
- Detect the
socks5://scheme and automatically useaiohttp-socks'sProxyConnector(if installed). - OR, allow a custom
connectorargument to be passed to its__init__to be used by the underlyingaiohttp.ClientSession.
Actual behaviour
The initialization fails. AiohttpRequest passes the proxy string directly to aiohttp.ClientSession, which does not natively support the SOCKS5 protocol.
aiohttp attempts to speak HTTP to the SOCKS5 proxy, which fails and closes the connection. This results in the following traceback:
telegram.error.NetworkError: aiohttp.ClientResponseError: 400, message="Expected HTTP/, RTSP/ or ICE/..."
Furthermore, attempting to work around this by manually creating a ProxyConnector from aiohttp-socks and passing it to AiohttpRequest is not possible, as it results in a TypeError:
TypeError: AiohttpRequest.__init__() got an unexpected keyword argument 'connector'
This makes it impossible to use SOCKS5 proxies with AiohttpRequest.
Workaround
The only workaround is to not use AiohttpRequest and use HTTPXRequest instead, which natively supports SOCKS5 when httpx[socks] is installed.
# This works
request_motor = HTTPXRequest(proxy_url=PROXY_URL)Configuration
Operating System:
Linux (running on Square Cloud)
Version of Python, python-telegram-bot & dependencies:
python-telegram-bot 22.5
Bot API 9.2
Python 3.11.9
Logs
❌ ERRO CRÍTICO NO STARTUP: aiohttp.ClientResponseError: 400, message="Expected HTTP/, RTSP/ or ICE/:\n\n b''\n ^", url='socks5://[credentials_hidden]'
Traceback (most recent call last):
File "/application/.local/lib/python3.13/site-packages/aiohttp/client_proto.py", line 315, in data_received
messages, upgraded, tail = self._parser.feed_data(data)
~~~~~~~~~~~~~~~~~~~~~~^^^^^^
File "aiohttp/_http_parser.pyx", line 556, in aiohttp._http_parser.HttpParser.feed_data
aiohttp.http_exceptions.BadHttpMessage: 400, message:
Expected HTTP/, RTSP/ or ICE/:
b''
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/application/.local/lib/python3.13/site-packages/aiohttp/client_reqrep.py", line 539, in start
message, payload = await protocol.read() # type: ignore[union-attr]
^^^^^^^^^^^^^^^^^^^^^
File "/application/.local/lib/python3.13/site-packages/aiohttp/streams.py", line 680, in read
await self._waiter
aiohttp.http_exceptions.HttpProcessingError: 400, message:
Expected HTTP/, RTSP/ or ICE/:
b''
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/application/.local/lib/python3.13/site-packages/ptbcontrib/aiohttp_request/aiohttprequest.py", line 209, in do_request
res = await self._session.request(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
File "/application/.local/lib/python3.13/site-packages/aiohttp/connector.py", line 1648, in _create_proxy_connection
resp = await proxy_resp.start(conn)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/application/.local/lib/python3.13/site-packages/aiohttp/client_reqrep.py", line 541, in start
raise ClientResponseError(
...
) from exc
aiohttp.client_exceptions.ClientResponseError: 400, message="Expected HTTP/, RTSP/ or ICE/:\n\n b''\n ^", url='socks5://[credentials_hidden]'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/application/./user_bot/main_user.py", line 95, in startup
await application.initialize()
...
File "/application/.local/lib/python3.13/site-packages/ptbcontrib/aiohttp_request/aiohttprequest.py", line 233, in do_request
raise NetworkError(f"aiohttp.{err.__class__.__name__}: {err}") from err
telegram.error.NetworkError: aiohttp.ClientResponseError: 400, message="Expected HTTP/, RTSP/ or ICE/:\n\n b''\n ^", url='socks5://[credentials_hidden]'