Can't configure proxy for Python Aiogram

144 views Asked by At

I'm trying to create a Telegram bot with Aiogram I live in Xi'An China where telgramm.org is blocked.

import asyncio

from aiogram import Dispatcher, Bot, F
from aiogram.types import Message

token = 'tokentokentokentokentokentokentokentoken'
bot = Bot(token, parse_mode='HTML')
dp = Dispatcher()

@dp.message(F.text == '/start')
async def start(message: Message):
    await message.answer(f'hi, <b>{message.from_user.first_name}</b>')

async def main():
    await dp.start_polling(bot)

if __name__ == '__main__':
    asyncio.run(main())

OUTPUT: aiogram.exceptions.TelegramNetworkError: HTTP Client says - ClientConnectorError: Cannot connect to host api.telegram.org:443 ssl:default [The semaphore timeout period has expired]

I use vpn, and can access this site in the browser no problem

import requests

response = requests.get('https://ipinfo.io/json')
print(response.text)
print(response.json()['country'])

OUTPUT: { "ip": "212.107.30.31", "city": "Hatsudai", "region": "Tokyo", "country": "JP", "loc": "35.6652,139.6701", "org": "AS41378 Kirino LLC", "postal": "155-0031", "timezone": "Asia/Tokyo", "readme": "https://ipinfo.io/missingauth" } JP

Not one of the dozens of proxy servers that i tried worked so far

import asyncio

from aiogram import Dispatcher, Bot, F
from aiogram.types import Message

from aiogram.client.session.aiohttp import AiohttpSession
session = AiohttpSession(proxy="socks5://98.162.25.16:4145/")

token = '6580655645:AAHSLO3gIQTSgGkHcERE7OEAtBfBWcEf9uw'
bot = Bot(token, session=session, parse_mode='HTML')
dp = Dispatcher()

@dp.message(F.text == '/start')
async def start(message: Message):
    await message.answer(f'hi, <b>{message.from_user.first_name}</b>')

async def main():
    await dp.start_polling(bot)

if __name__ == '__main__':
    asyncio.run(main())

OUTPUT: python_socks._errors.ProxyConnectionError: [Errno 22] Couldn't connect to proxy 98.162.25.16:4145 [The semaphore timeout period has expired]

The requests module appears to just ignore proxies altogether

import requests

proxies = {'socks5': "socks5://98.162.25.16:4145"}

response = requests.get('https://ipinfo.io/json', proxies=proxies)
print(response.text)
print(response.json()['country'])

OUTPUT: { "ip": "212.107.30.31", "city": "Hatsudai", "region": "Tokyo", "country": "JP", "loc": "35.6652,139.6701", "org": "AS41378 Kirino LLC", "postal": "155-0031", "timezone": "Asia/Tokyo", "readme": "https://ipinfo.io/missingauth" } JP

0

There are 0 answers