2017-07-24 48 views
0

我正在学习python中的一些异步/等待,我想尝试它,但是 我在尝试连接到chatango时遇到此错误websocket,我不知道是什么意思。aiohttp.client_exceptions.ClientResponseError:400,message ='无效常量字符串'

我使用python 3.6.1和2.2.3 aiohttp

这是我的代码:

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' 
+0

是否为''ws://s12.chatango.com:8081 /“'是正确的?我在这个问题上读到并得出结论,也许你的资源是错误的... –

+0

这是正确的@YuvalPruss – TheClonerx

回答

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)