Изучаю использование прокси в библиотеках requests и aiohttp.
Написал небольшой тестовый код, но не могу понять, почему использование proxy в библиотеке aiohttp выдает ошибку.
Для теста купил прокси, при покупке предоставили сведения о том, какие порты использовать:
http/https 45785
socks5 45786
Вот собственно сам код:
import requests
import asyncio
import aiohttp
from bs4 import BeautifulSoup
from aiohttp_proxy import ProxyConnector
headers = {
'User-Agent' : 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.119 YaBrowser/22.3.0.2430 Yowser/2.5 Safari/537.36'
}
proxies = {
'https' : 'http://user:pass@5.188.181.182:45785',
}
async def get_location(url):
response = requests.get(url=url, headers=headers, proxies=proxies)
soup = BeautifulSoup(response.text, 'lxml')
ip = soup.find('div', class_='ip').text.strip()
location = soup.find('div', class_='value value-country').text.strip()
print(f'ПРОВЕРКА ПРОКСИ В REQUESTS:\nIP: {ip}\nLocation: {location}')
print()
connector = ProxyConnector.from_url('http://user:pass@5.188.181.182:45785')
async with aiohttp.ClientSession(connector=connector) as session:
async with session.get(url, headers=headers) as response:
text = await response.text()
soup = BeautifulSoup(text, 'lxml')
ip = soup.find('div', class_='ip').text.strip()
location = soup.find('div', class_='value value-country').text.strip()
print(f'ПРОВЕРКА ПРОКСИ В AIOHTTP:\nIP: {ip}\nLocation: {location}')
async def main():
await get_location('https://2ip.ru')
if __name__ == '__main__':
asyncio.run(main())
Часть кода с использованием requests работает нормально, но при использовании aiohttp - выдается ошибка о том, что параметр задан неверно:
ПРОВЕРКА ПРОКСИ В REQUESTS:
IP: 5.188.181.182
Location: Испания, Мадрид
Traceback (most recent call last):
File "C:\Users\79204\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\connector.py", line 969, in _wrap_create_connection
return await self._loop.create_connection(*args, **kwargs) # type: ignore # noqa
File "C:\Users\79204\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 1081, in create_connection
transport, protocol = await self._create_connection_transport(
File "C:\Users\79204\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 1111, in _create_connection_transport
await waiter
File "C:\Users\79204\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 395, in _loop_writing
self._write_fut = self._loop._proactor.send(self._sock, data)
File "C:\Users\79204\AppData\Local\Programs\Python\Python39\lib\asyncio\windows_events.py", line 529, in send
self._register_with_iocp(conn)
File "C:\Users\79204\AppData\Local\Programs\Python\Python39\lib\asyncio\windows_events.py", line 718, in _register_with_iocp
_overlapped.CreateIoCompletionPort(obj.fileno(), self._iocp, 0, 0)
OSError: [WinError 87] Параметр задан неверно
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\MyProjects\tst.py", line 38, in <module>
asyncio.run(main())
File "C:\Users\79204\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Users\79204\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
return future.result()
File "D:\MyProjects\tst.py", line 35, in main
await get_location('https://2ip.ru')
File "D:\MyProjects\tst.py", line 26, in get_location
async with session.get(url, headers=headers) as response:
File "C:\Users\79204\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\client.py", line 1117, in __aenter__
self._resp = await self._coro
File "C:\Users\79204\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\client.py", line 520, in _request
conn = await self._connector.connect(
File "C:\Users\79204\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp_proxy\connector.py", line 100, in connect
return await super(ProxyConnector, self).connect(
File "C:\Users\79204\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\connector.py", line 535, in connect
proto = await self._create_connection(req, traces, timeout)
File "C:\Users\79204\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\connector.py", line 890, in _create_connection
_, proto = await self._create_proxy_connection(req, traces, timeout)
File "C:\Users\79204\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\connector.py", line 1139, in _create_proxy_connection
transport, proto = await self._wrap_create_connection(
File "C:\Users\79204\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp_proxy\connector.py", line 91, in _wrap_create_connection
return await super(ProxyConnector, self)._wrap_create_connection(
File "C:\Users\79204\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\connector.py", line 975, in _wrap_create_connection
raise client_error(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host 2ip.ru:443 ssl:default [Параметр задан неверно]
В чем ошибка и как ее можно исправить?