使用aiohttp客户端遇到麻烦。
问题:我需要通过https url下载图像,它在requests.get()下可以很好地工作,但是由于aiohttp超时而失败。

这是失败的示例:

url = "https://www.miamiherald.com/wps/source/images/miamiherald/facebook.jpg"
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}

async with aiohttp.ClientSession().get(url, headers=headers) as response:
    content = await response.read()


得到了:

Fatal read error on socket transport
protocol: <asyncio.sslproto.SSLProtocol object at 0x10fd8be80>
transport: <_SelectorSocketTransport fd=11 read=polling write=<idle, bufsize=0>>
Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/selector_events.py", line 801, in _read_ready__data_received
    data = self._sock.recv(self.max_size)
TimeoutError: [Errno 60] Operation timed out


同时,requests与相同的标头可以正常工作!

url = "https://www.miamiherald.com/wps/source/images/miamiherald/facebook.jpg"
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}

r = requests.get(url, headers = headers, stream=True)


谁能帮助我使它适用于此特定示例?

最佳答案

只需将超时arg传递给cs.get(timeout=...)cs(timeout=...)。这是文档https://docs.aiohttp.org/en/stable/client_quickstart.html#timeouts

例:

import asyncio
import aiohttp
from aiohttp.client import ClientTimeout


async def test():
    url = "https://www.google.com/photos/about/static/images/google.svg"
    headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
    }

    timeout = ClientTimeout(total=0)  # `0` value to disable timeout
    cs = aiohttp.ClientSession()

    async with cs.request('get', url, headers=headers, timeout=timeout) as response:
        print(response.status)
        content = await response.read()

    await cs.close()


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(test())

关于python - 由于User-Agent问题,AIOHTTP客户端超时,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56769084/

10-11 10:19