0

Recently, I'm looking at the python aiohttp lib, play around it, compare with python requests. Here is the code:

import aiohttp
import asyncio
import requests

request_url = 'http://www.baidu.com'
requests_resp = requests.get(request_url)

async def fetch(session, url):
    async with session.get(url) as response:
        return await response.text()

async def main():
    async with aiohttp.ClientSession() as session:
        aio_resp = await fetch(session, request_url)
        print('aio_resp_length =', len(aio_resp))

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

print('requests_resp_length = ', len(requests_resp.text))

The response lengths with a huge diffferences

aio_resp_length = 152576
requests_resp_length =  2381

Not sure what happens in aiohttp.session.get, but this result is not always like this. When you change the requests_url to http://www.example.com,the response lengthes are the same. Can someone tell me what happened here?

Cheers

Tide
  • 3
  • 5
  • Have you tried comparing/looking at the actual content, not just its length…? – deceze Jan 21 '19 at 13:24
  • @deceze, yes, the content of aiohttp, with the same content as the 'view page source' in your browser. but the response from requests, it doesn't have the content of css files. Is this beacuse website do some kinds of operations for long-live requests, that could return the total css's content? – Tide Jan 22 '19 at 03:10

1 Answers1

0

Because aiohttp has newline in it's response and requests doesn't.

you can check thier response like this

print('requests_resp_length = ', requests_resp.text[0:100])

print('aio_resp_length =', aio_resp[0:100])
Vignesh Krishnan
  • 743
  • 8
  • 15
  • Why does one have newlines and the other doesn't? And yes, the first hundred characters of both will have the same length, 100… – deceze Jan 21 '19 at 13:57
  • What I'm curious about is the difference content between these two libraries. – Tide Jan 22 '19 at 03:13