2017-07-24 78 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)