Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aiohttp error invalid constant string

Tags:

python

aiohttp

#!/usr/bin/env python3.5

import asyncio
import aiohttp

url = "http://eniig.dk"

async def main():
    try:
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as response:
                try:
                    body = await response.read()
                    print(response.method)
                    print(body)
                except UnicodeDecodeError as msg:
                    print(msg)
                    print(body)
    except Exception as msg:
        print(msg)


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

This code gives an error:

400, message='400, message='invalid constant string''

If the URL specifies 'https', then everything is OK. But how to avoid an error if the protocol is not known in advance?

What alternatives will advise aiohttp?

UPD: I have not been here for a long time. Forgot to say that the code was run under Debian 8 stable. The problem was solved by replacing the methods ClientSession() and get() by TCPConnector() and request()

like image 800
Grigory Avatar asked Nov 26 '25 01:11

Grigory


1 Answers

It's looks like a pretty good code. Worked for me with this versions of aiohttp:

  • 2.0.7
  • 2.0.6
  • 2.0.5
  • 2.0.4
  • 2.0.3
  • 2.0.2
  • 2.0.1
  • 2.0.0
like image 104
Yuval Pruss Avatar answered Nov 27 '25 15:11

Yuval Pruss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!