python - aiohttp 错误无效常量字符串

标签 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()

这段代码报错:

400, message='400, message='无效常量字符串''

如果 URL 指定“https”,则一切正常。但是,如果事先不知道协议(protocol),如何避免错误呢?

什么替代方案会建议 aiohttp?

更新: 我已经很久没有来过这里了。忘了说代码是在 Debian 8 stable 下运行的。通过将 ClientSession() 和 get() 方法替换为 TCPConnector() 和 request() 解决了这个问题

最佳答案

代码看起来不错。 使用此版本的 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

关于python - aiohttp 错误无效常量字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43306148/

相关文章:

python - aiohttp 限制并发请求数

python - 在列表中映射字典键值

python - 在 Python 中聚合简单字典的最佳方法

python - 为什么 to_sql 不能与 pandas 中的 pyodbc 一起使用?

python-3.x - 无法停止 aiohttp websocket 服务器

python - aiohttp.ClientSession().post

python - 如何在 discord.py 中设置可选参数?

python - get genfromtxt/loadtxt 忽略被忽略的列/行中的数据类型

python - 立即处理异步响应

python - 使用 aiohttp 的 HEAD 请求很慢