requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='api.telegram.org', port=443): # python 使用 pytelegrambotapi 和 Tor 的电报机器人

requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='api.telegram.org', port=443): # python telegram bot using pytelegrambotapi and Tor

当我想使用 Tor 连接到电报机器人时 returns 错误发生

requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot/getUpdates?offset=1&timeout=20 (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x7f28923f4a30>, 'Connection to api.telegram.org timed out. (connect timeout=20)'))

首先使用 sudo apt install tor 安装 tor 然后运行 comeds blow

sudo apt install privoxy torsocks

nano /etc/privoxy/config

然后 pip install pysocks 在包含 forward-socks5t / 127.0.0.1:9050 .

的 uncommnet 行之后

现在

sudo systemctl enable privoxy.service
sudo systemctl start privoxy.service

现在确保在 /etc/tor/torsocks.conf 文件中取消对 blow 的表扬

TorAddress 127.0.0.1
TorPort 9050

重启系统

接下来在你的机器人源代码中添加代码

from telebot import apihelper

apihelper.proxy = {'https': 'socks5h://127.0.0.1:9050',
#    'http':'http://127.0.0.1:8118',
#    'https':'https://127.0.0.1:8118'
}