Python aiohttp returns 与 python 请求不同的响应。我需要帮助理解为什么

Python aiohttp returns a different reponse than python requests. I need help understanding why

经过昨天的整个晚上和今天的早上,我真的帮助理解了为什么 aiohttp 请求 returns 不同于 requests 请求。

import requests

reqUrl = "https://api-mainnet.magiceden.io/all_collections_with_escrow_data"

headersList = {
 "Accept": "*/*",
 " User-Agent" : " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36" 
}

payload = ""

response = requests.request("GET", reqUrl, data=payload,  headers=headersList)

print(response.text)

returns全部内容 {"collections":[{"符号"......

import aiohttp
import asyncio 

headersList = {
            'authority': 'api-mainnet.magiceden.io',
            'Accept': 'application/json, text/plain, */*',
            'accept-language': 'en-US,en;q=0.9',
            'origin': 'https://magiceden.io',
            'referer': 'https://magiceden.io/',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-site',
            'sec-gpc': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36',
        }

payload = ""

async def test():
    async with aiohttp.ClientSession() as session:
        get_image_data = await session.get(
                            'https://api-mainnet.magiceden.io/all_collections_with_escrow_data', headers=headersList)
        result = get_image_data.text

        print(result)
    
if __name__ == '__main__':
    asyncio.run(test())

returns:

<bound method ClientResponse.text of <ClientResponse(https://api-mainnet.magiceden.io/all_collections_with_escrow_data) [200 OK]>
<CIMultiDictProxy('Date': 'Thu, 02 Jun 2022 12:43:22 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'X-Powered-By': 'Express', 'X-RateLimit-Limit': '120', 'X-RateLimit-Remaining': '119', 'X-RateLimit-Reset': '1654173863', 'Access-Control-Allow-Origin': 'https://magiceden.io', 'Vary': 'Origin', 'Access-Control-Allow-Credentials': 'true', 'Cache-Control': 'public, max-age=300, s-maxage=300', 'CDN-Cache-Control': 'public, max-age=300, s-maxage=300', 'Etag': 'W/"4f27d7-Cndhwdfejd0aSIGFdSQriuQfbvE"', 'Set-Cookie': 'connect.sid=s%3AcUUsXzow-3-5kuLPJcNNndd5zVxtCIvc.ggQdFm%2FooB%2FpWho%2FqYiVWJQa4vCtQ9VZGRisUqFXigw; Domain=magiceden.io; Path=/; Expires=Thu, 02 Jun 2022 12:53:22 GMT; HttpOnly', 'X-Kong-Upstream-Latency': '242', 'X-Kong-Proxy-Latency': '0', 'Via': 'kong/2.7.2', 'CF-Cache-Status': 'DYNAMIC', 'Expect-CT': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"', 'Set-Cookie': '__cf_bm=i5THtMhPjqGPXy8zFyXSP43DLHQESpOLbWff7n_W6qE-1654173802-0-AQkuHSP7Sv+YRRSr1wmUBDKb5EOjcAiPVXyx7lvqe0NF2wmLHcFK9JFLPVkiuTpMOjp/wpiMpQU377nAimriGP0=; path=/; expires=Thu, 02-Jun-22 13:13:22 GMT; domain=.magiceden.io; HttpOnly; Secure; SameSite=None', 'Server': 'cloudflare', 'CF-RAY': '715046367a9b0219-ZRH', 'Content-Encoding': 'gzip', 'alt-svc': 'h3=":443"; ma=86400, h3-29=":443"; ma=86400')>
>

谁能帮我理解为什么? 非常感谢你们...

文本实际上位于 content 属性中的 StreamReader 对象中

get_image_data = await session.get('https://api-mainnet.magiceden.io/all_collections_with_escrow_data', headers=headersList)
stream = get_image_data.content
data = await stream.read()
print(data)

您看到的输出是因为 text 应该作为(异步)方法调用,而不是作为属性查找。

如果您只想将完整的响应正文作为字符串,则需要 await response.text()official aiohttp tutorial 所示:

async def test():
    async with aiohttp.ClientSession() as session:
        async with session.get(url, headers=headers_list) as response:
            text = await response.text()

    print(text)

requests.getaiohttp.ClientSession.get 之间的一个区别是 requests 会立即获取整个响应主体并记住它,但 aiohttp 不会。 aiohttp 让您忽略正文,或分块阅读,或在查看 headers/status 代码后阅读。

这就是为什么您需要再做一次 awaitaiohttp 需要做更多 I/O 才能获得响应正文。