Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cant use proxies in telegram-python-bot [BUG] #4263

Open
MoSi8160 opened this issue May 20, 2024 · 5 comments
Open

cant use proxies in telegram-python-bot [BUG] #4263

MoSi8160 opened this issue May 20, 2024 · 5 comments
Labels

Comments

@MoSi8160
Copy link

MoSi8160 commented May 20, 2024

Steps to Reproduce

i use python telegram bot and in my country, telegram is filtered and no one can access.
i want to use proxy buy i always get timed out error ( i checked my http proxy and its work )

my code:

application = (Application.builder()
                       .token(self.token)
                       .proxy('http://proxy_url:proxy_port'))
application.run_polling(allowed_updates=Update.ALL_TYPES)

Expected behaviour

When the proxy is set, all connections must be made through the proxy

Actual behaviour

I receive an error that I put the full text of the error in the relevant section

Operating System

python 3.11.9

Version of Python, python-telegram-bot & dependencies

aiofiles            23.2.1
aiohttp             3.9.5
aiosignal           1.3.1
anyio               4.3.0
asgiref             3.8.1
attrs               23.2.0
beautifulsoup4      4.12.3
certifi             2024.2.2
charset-normalizer  3.3.2
colorama            0.4.6
decorator           4.4.2
dj-database-url     2.1.0
Django              4.2
django-extensions   3.2.3
django-jalali       6.2.1
django-resized      1.0.2
frozenlist          1.4.1
h11                 0.14.0
httpcore            1.0.5
httpx               0.27.0
idna                3.7
imageio             2.34.1
imageio-ffmpeg      0.4.9
jalali_core         1.0.0
jdatetime           5.0.0
markdownify         0.12.1
moviepy             1.0.3
multidict           6.0.5
mutagen             1.47.0
numpy               1.26.4
opencv-python       4.9.0.80
pillow              10.3.0
pip                 24.0
proglog             0.1.10
pycryptodome        3.20.0
pydantic            1.10.12
PySocks             1.7.1
python-telegram-bot 21.1.1
requests            2.31.0
rubpy               7.0.5
setuptools          65.5.0
six                 1.16.0
sniffio             1.3.1
soupsieve           2.5
sqlparse            0.5.0
tqdm                4.66.4
typing_extensions   4.11.0
tzdata              2024.1
urllib3             2.2.1
yarl                1.9.4

Relevant log output

Traceback (most recent call last):
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_transports\default.py", line 69, in map_httpcore_exceptions    
    yield
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_transports\default.py", line 373, in handle_async_request      
    resp = await self._pool.handle_async_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_async\connection_pool.py", line 216, in handle_async_request
    raise exc from None
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_async\connection_pool.py", line 196, in handle_async_request
    response = await connection.handle_async_request(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_async\http_proxy.py", line 289, in handle_async_request
    connect_response = await self._connection.handle_async_request(
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_async\connection.py", line 101, in handle_async_request
    return await self._connection.handle_async_request(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_async\http11.py", line 143, in handle_async_request
    raise exc
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_async\http11.py", line 113, in handle_async_request
    ) = await self._receive_response_headers(**kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_async\http11.py", line 186, in _receive_response_headers
    event = await self._receive_event(timeout=timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_async\http11.py", line 224, in _receive_event
    data = await self._network_stream.read(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_backends\anyio.py", line 32, in read
    with map_exceptions(exc_map):
  File "C:\Users\PC2\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 158, in __exit__
    self.gen.throw(typ, value, traceback)
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc) from exc
httpcore.ReadTimeout

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\request\_httpxrequest.py", line 276, in do_request
    res = await self._client.request(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_client.py", line 1574, in request
    return await self.send(request, auth=auth, follow_redirects=follow_redirects)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_client.py", line 1661, in send
    response = await self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_client.py", line 1689, in _send_handling_auth
    response = await self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_client.py", line 1726, in _send_handling_redirects
    response = await self._send_single_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_client.py", line 1763, in _send_single_request
    response = await transport.handle_async_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_transports\default.py", line 372, in handle_async_request
    with map_httpcore_exceptions():
  File "C:\Users\PC2\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 158, in __exit__
    self.gen.throw(typ, value, traceback)
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_transports\default.py", line 86, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ReadTimeout

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "d:\MaMaD\پیام رسان سیمرغ\messanger\manage.py", line 346, in <module>
    a = base_bot(token="TOKEN").run()
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\MaMaD\پیام رسان سیمرغ\messanger\manage.py", line 343, in run
    application.run_polling(allowed_updates=Update.ALL_TYPES)
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\ext\_application.py", line 838, in run_polling
    return self.__run(
           ^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\ext\_application.py", line 1049, in __run
    raise exc
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\ext\_application.py", line 1038, in __run
    loop.run_until_complete(self.initialize())
  File "C:\Users\PC2\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\ext\_application.py", line 497, in initialize
    await self.bot.initialize()
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\ext\_extbot.py", line 295, in initialize
    await super().initialize()
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\_bot.py", line 763, in initialize
    await self.get_me()
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\ext\_extbot.py", line 1880, in get_me
    return await super().get_me(
           ^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\_bot.py", line 896, in get_me
    result = await self._post(
             ^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\_bot.py", line 615, in _post
    return await self._do_post(
           ^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\ext\_extbot.py", line 349, in _do_post
    return await super()._do_post(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\_bot.py", line 644, in _do_post
    result = await request.post(
             ^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\request\_baserequest.py", line 200, in post
    result = await self._request_wrapper(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\request\_baserequest.py", line 340, in _request_wrapper
    raise exc
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\request\_baserequest.py", line 330, in _request_wrapper
    code, payload = await self.do_request(
                    ^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\request\_httpxrequest.py", line 293, in do_request
    raise TimedOut from err
telegram.error.TimedOut: Timed out

Additional Context

No response

@Bibo-Joshi
Copy link
Member

Hi.

When the proxy is set, all connections must be made through the proxy

Your provided traceback does not give reason to think that the proxy is not being used. Quite the contrary, the line

File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_async\http_proxy.py", line 289, in handle_async_request
    connect_response = await self._connection.handle_async_request(

does look very much like the httpx library (which ptb uses for the networking stuff) is using your proxy.

So far I only see a regular timeout exception. Please have a look at this wiki page for that

@harshil21
Copy link
Member

Also you should use get_updates_proxy_url as well. See this wiki page.

@MoSi8160
Copy link
Author

MoSi8160 commented May 21, 2024

Also you should use get_updates_proxy_url as well. See this wiki page.

i used this but problem exist
NOTE : my proxy url is something like : "http://ip:port". (its not "http://user:pass@ip:port"

I don't think PTB uses the proxy I define at all because there is no difference in the error between when I use it and when I don't.

code:

application = (Application.builder()
                       .token(self.token)
                       .proxy('proxy')
                       .get_updates_proxy_url('proxy'))
application = application.build()
....
application.run_polling(allowed_updates=Update.ALL_TYPES)

traceback:

Traceback (most recent call last):
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_transports\default.py", line 69, in map_httpcore_exceptions    
    yield
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_transports\default.py", line 373, in handle_async_request      
    resp = await self._pool.handle_async_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_async\connection_pool.py", line 216, in handle_async_request
    raise exc from None
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_async\connection_pool.py", line 196, in handle_async_request
    response = await connection.handle_async_request(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_async\http_proxy.py", line 289, in handle_async_request
    connect_response = await self._connection.handle_async_request(
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_async\connection.py", line 101, in handle_async_request
    return await self._connection.handle_async_request(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_async\http11.py", line 143, in handle_async_request
    raise exc
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_async\http11.py", line 113, in handle_async_request
    ) = await self._receive_response_headers(**kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_async\http11.py", line 186, in _receive_response_headers
    event = await self._receive_event(timeout=timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_async\http11.py", line 224, in _receive_event
    data = await self._network_stream.read(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_backends\anyio.py", line 32, in read
    with map_exceptions(exc_map):
  File "C:\Users\PC2\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 158, in __exit__
    self.gen.throw(typ, value, traceback)
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpcore\_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc) from exc
httpcore.ReadTimeout

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\request\_httpxrequest.py", line 276, in do_request
    res = await self._client.request(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_client.py", line 1574, in request
    return await self.send(request, auth=auth, follow_redirects=follow_redirects)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_client.py", line 1661, in send
    response = await self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_client.py", line 1689, in _send_handling_auth
    response = await self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_client.py", line 1726, in _send_handling_redirects
    response = await self._send_single_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_client.py", line 1763, in _send_single_request
    response = await transport.handle_async_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_transports\default.py", line 372, in handle_async_request
    with map_httpcore_exceptions():
  File "C:\Users\PC2\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 158, in __exit__
    self.gen.throw(typ, value, traceback)
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\httpx\_transports\default.py", line 86, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ReadTimeout

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "d:\MaMaD\پیام رسان سیمرغ\messanger\manage.py", line 351, in <module>
    a = base_bot(token="TOKEN").run()
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\MaMaD\پیام رسان سیمرغ\messanger\manage.py", line 348, in run
    application.run_polling(allowed_updates=Update.ALL_TYPES)
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\ext\_application.py", line 838, in run_polling
    return self.__run(
           ^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\ext\_application.py", line 1049, in __run
    raise exc
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\ext\_application.py", line 1038, in __run
    loop.run_until_complete(self.initialize())
  File "C:\Users\PC2\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\ext\_application.py", line 497, in initialize
    await self.bot.initialize()
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\ext\_extbot.py", line 295, in initialize
    await super().initialize()
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\_bot.py", line 763, in initialize
    await self.get_me()
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\ext\_extbot.py", line 1880, in get_me
    return await super().get_me(
           ^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\_bot.py", line 896, in get_me
    result = await self._post(
             ^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\_bot.py", line 615, in _post
    return await self._do_post(
           ^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\ext\_extbot.py", line 349, in _do_post
    return await super()._do_post(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\_bot.py", line 644, in _do_post
    result = await request.post(
             ^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\request\_baserequest.py", line 200, in post
    result = await self._request_wrapper(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\request\_baserequest.py", line 340, in _request_wrapper
    raise exc
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\request\_baserequest.py", line 330, in _request_wrapper
    code, payload = await self.do_request(
                    ^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MaMaD\پیام رسان سیمرغ\messanger\.venv\Lib\site-packages\telegram\request\_httpxrequest.py", line 293, in do_request
    raise TimedOut from err
telegram.error.TimedOut: Timed out

@MoSi8160
Copy link
Author

i know where is my problem
how can i tell httpx to resolve dns from proxy side ?
something like: socks5h://user:pass@ip:port

@harshil21
Copy link
Member

try manually setting a proxy and making a request first - https://www.python-httpx.org/advanced/proxies/

how can i tell httpx to resolve dns from proxy side ?

That's handled by httpx internally probably. I can't speak for that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants