aiohttp.client_exceptions.ClientResponseError:400,message ='无效常量字符串'

时间:2017-07-24 05:36:55

标签: python python-3.6 aiohttp

我在python中学习了一些async / await,我想尝试一下,但是 我试图通过websocket连接到chatango时遇到此错误,我不知道是什么意思。

我正在使用python 3.6.1和aiohttp 2.2.3

这是我的代码:

import asyncio
import aiohttp
msgs = []
async def main():
    async with  aiohttp.ClientSession() as session:
        async with session.ws_connect("ws://s12.chatango.com:8081/") as ws:
            for msg in ws:
                msgs.append(msg)
                print(msg)

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

完整追溯:

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\client_reqrep.py", line 559, in start
    (message, payload) = yield from self._protocol.read()
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\streams.py", line 509, in read
    yield from self._waiter
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\client_proto.py", line 165, in data_received
    messages, upgraded, tail = self._parser.feed_data(data)
  File "aiohttp\_http_parser.pyx", line 274, in aiohttp._http_parser.HttpParser.feed_data (aiohttp/_http_parser.c:4364)
aiohttp.http_exceptions.BadHttpMessage: 400, message='invalid constant string'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:/Users/joseh/Desktop/a.ws.py", line 42, in <module>
    loop.run_until_complete(main())
  File "C:\Program Files\Python36\lib\asyncio\base_events.py", line 466, in run_until_complete
    return future.result()
  File "C:/Users/joseh/Desktop/a.ws.py", line 34, in main
    async with session.ws_connect("ws://s12.chatango.com:8081/") as ws:
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\client.py", line 603, in __aenter__
    self._resp = yield from self._coro
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\client.py", line 390, in _ws_connect
    proxy_auth=proxy_auth)
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\helpers.py", line 91, in __iter__
    ret = yield from self._coro
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\client.py", line 241, in _request
    yield from resp.start(conn, read_until_eof)
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\client_reqrep.py", line 564, in start
    message=exc.message, headers=exc.headers) from exc
aiohttp.client_exceptions.ClientResponseError: 400, message='invalid constant string'

1 个答案:

答案 0 :(得分:0)

invalid constant string是来自chatango的自定义响应,他们可能需要协议或某种auth标头。

如果你不太了解chatango如何使用websockets,那么对他们的系统进行逆向工程可能不是学习asyncio和aiohttp的好任务。

最好使用类似httparrot之类的东西来回复你发送的消息。

这是你的代码被修改为使用httparrot并发送5条消息,获得5条响应,然后退出。

import asyncio
import aiohttp
msgs = []

async def main():
    async with  aiohttp.ClientSession() as session:
        async with session.ws_connect('ws://httparrot.herokuapp.com/websocket') as ws:
            ws.send_str('hello')
            async for msg in ws:
                msgs.append(msg)
                print(msg)
                ws.send_str('hello')
                if len(msgs) >= 5:
                    break


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