无法将aiohttp请求转换为文本,同时使用rule34 API。

huangapple go评论235阅读模式
英文:

Can't convert aiohttp request to text while using rule34 api

问题

  1. 我想使用 aiohttp 来向 rule34 API 发出请求但是当我使用 `text()` `json()` 方法时程序会冻结
  2. ```python
  3. import aiohttp
  4. import asyncio
  5. async def foo():
  6. async with aiohttp.ClientSession() as session:
  7. r = await session.get('https://api.rule34.xxx/index.php?page=dapi&s=post&q=index&json=1&limit=1000')
  8. print('got response') #ok
  9. text = await r.text() #freezes
  10. print(text) #never reaches
  11. asyncio.run(foo())

但是当我在 URL 中使用 limit=1 时,它正常工作。

而且,使用 requests 库和任何限制值也可以正常工作:

  1. import requests
  2. r = requests.get('https://api.rule34.xxx/index.php?page=dapi&s=post&q=index&json=1&limit=1000')
  3. print(r.text)

我尝试使用调试器,注意到在调用 await r.text() 之前,r._bodyNone,我需要的数据位于 r.content._buffer 中,看起来像是 deque([b'data']),因此我尝试使用它,但后来我注意到数据不完整,就好像没有完全下载:

无法将aiohttp请求转换为文本,同时使用rule34 API。

也许是因为 r 没有被等待或者其他原因,但我不能使用 await r,因为它会抛出异常,或者 await r.text(),因为它会冻结。所以我甚至找不到发生这种情况的原因。

  1. <details>
  2. <summary>英文:</summary>
  3. I wanted to make a request to rule34 api using aiohttp, but when I use `text()` or `json()` method the program freezes:
  4. ```python
  5. import aiohttp
  6. import asyncio
  7. async def foo():
  8. async with aiohttp.ClientSession() as session:
  9. r = await session.get(&#39;https://api.rule34.xxx/index.php?page=dapi&amp;s=post&amp;q=index&amp;json=1&amp;limit=1000&#39;)
  10. print(&#39;got response&#39;) #ok
  11. text = await r.text() #freezes
  12. print(text) #never reaches
  13. asyncio.run(foo())

But when I use limit=1 in URL it works normally.

Also it works with requests library with any limit value:

  1. import requests
  2. r = requests.get(&#39;https://api.rule34.xxx/index.php?page=dapi&amp;s=post&amp;q=index&amp;json=1&amp;limit=1000&#39;)
  3. print(r.text)

I tried to use debugger and noticed that r._body was None before await r.text() was called and the data I need is located at r.content._buffer and looks like deque([b&#39;data&#39;]), so I tried to use it, but then I noticed that the data isn't complete, like it isn't downloaded completely:

无法将aiohttp请求转换为文本,同时使用rule34 API。.

Maybe because of the r wasn't awaited or I don't know, but I can't use await r because it throws an exception, or await r.text() because it freezes. So I can't even find the reason why it happens.

答案1

得分: 0

您与aiohttp的所有交互都需要在ClientSession上下文管理器内完成,所以您需要(请注意缩进的更改):

  1. import aiohttp
  2. import asyncio
  3. async def foo():
  4. async with aiohttp.ClientSession() as session:
  5. r = await session.get('https://api.rule34.xxx/index.php?page=dapi&s=post&q=index&json=1&limit=1000')
  6. print('得到响应')
  7. text = await r.text()
  8. print(text)
  9. asyncio.run(foo())

这将成功打印来自API的JSON响应。

英文:

All of your interaction with aiohttp needs to be inside the ClientSession context manager, so you need (note the change in indentation):

  1. import aiohttp
  2. import asyncio
  3. async def foo():
  4. async with aiohttp.ClientSession() as session:
  5. r = await session.get(&#39;https://api.rule34.xxx/index.php?page=dapi&amp;s=post&amp;q=index&amp;json=1&amp;limit=1000&#39;)
  6. print(&#39;got response&#39;)
  7. text = await r.text()
  8. print(text)
  9. asyncio.run(foo())

This successfully prints a JSON response from the API.

huangapple
  • 本文由 发表于 2023年6月30日 05:25:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定