Вот мой код
import requests
import aiohttp, asyncio
url = 'https://www.alliedelec.com/automation-control/control-station/'
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}
text1 = requests.get(url, headers=headers).text
print(text1)
async def fun():
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as resp:
text2 = await resp.text()
print(text2)
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.ensure_future(fun()))
requests
может получить правильные результаты, правильный HTML исходный код. Но aiohttp получает
aiohttp.client_exceptions.ClientOSError
.
Вот трассировка:
Traceback (most recent call last):
File "E:/my/test.py", line 17, in <module>
loop.run_until_complete(asyncio.ensure_future(fun()))
File "C:\Program Files\Python36\lib\asyncio\base_events.py", line 468, in run_until_complete
return future.result()
File "E:/my/test.py", line 12, in fun
async with session.get(url, headers=headers) as resp:
File "D:\manyvenv\tornado_api_server\lib\site-packages\aiohttp\client.py", line 1005, in __aenter__
self._resp = await self._coro
File "D:\manyvenv\tornado_api_server\lib\site-packages\aiohttp\client.py", line 497, in _request
await resp.start(conn)
File "D:\manyvenv\tornado_api_server\lib\site-packages\aiohttp\client_reqrep.py", line 844, in start
message, payload = await self._protocol.read() # type: ignore # noqa
File "D:\manyvenv\tornado_api_server\lib\site-packages\aiohttp\streams.py", line 588, in read
await self._waiter
aiohttp.client_exceptions.ClientOSError: [WinError 10054] An existing connection was forcibly closed by the remote host
В чем разница между запросом запросов и запросом aiohttp?
python == 3.6.5 , aiohttp == 3.5.4, запросы == 2.23.0
СПАСИБО!