asyncins / aiowebsocket

Async WebSocket Client. Advantage: Flexible Lighter and Faster
321 stars 55 forks source link

添加自定义头后不能运行,不添加自定义头会被封 #5

Open zky97 opened 5 years ago

zky97 commented 5 years ago

import asyncio import logging from datetime import datetime from aiowebsocket.converses import AioWebSocket async def startup(uri,problem): header=[ 'GET wss://www.hfwh.top/ HTTP/1.1', 'Host: www.hfwh.top', 'Origin: https://www.woxunbudao.cn', 'Upgrade: websocket', 'Sec-WebSocket-Version: 13', 'Connection: Upgrade', 'Sec-WebSocket-Accept: g+8vNmGT8HaLHnj3QcQ/+0Nr5rM=', ] async with AioWebSocket(uri,headers=header) as aws: converse = aws.manipulator Problem=problem message = '{"type":1,"chars":"'+Problem+'"}' print(message) await converse.send(message)#发送数据 print('Client send: {message} \n'.format(message=Problem)) mes = await converse.receive()#接收数据 print('Client receive: {rec} \n'.format(rec=mes)) return mes if name=='main': remote='wss://www.hfwh.top/' problem='科举中"殿试"的主考官是' print(problem) print(remote) try: asyncio.get_event_loop().run_until_complete(startup(remote,problem)) except KeyboardInterrupt as exc: logging.info('Quit.')

asyncins commented 5 years ago

不能运行,指的是会报错吗?贴报错信息看一下

zky97 commented 5 years ago

直接就没显示也没报错,用你的带头的列子也是一样,直接没一点内容输出,也没报错,我用的是py3.7

asyncins commented 5 years ago

直接就没显示也没报错,用你的带头的列子也是一样,直接没一点内容输出,也没报错,我用的是py3.7

没有报错就是没问题。没有输出很有可能是目标网站还做了其他的验证,再检查一下。

otccoin commented 5 years ago

Traceback (most recent call last): File "huobi-asyncio-ws.py", line 33, in asyncio.get_event_loop().run_until_complete(startup(remote)) File "/Users/chengxuan/anaconda3/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete return future.result() File "huobi-asyncio-ws.py", line 17, in startup result=gzip.decompress(msg).decode('utf-8') File "/Users/chengxuan/anaconda3/lib/python3.7/gzip.py", line 532, in decompress return f.read() File "/Users/chengxuan/anaconda3/lib/python3.7/gzip.py", line 276, in read return self._buffer.read(size) File "/Users/chengxuan/anaconda3/lib/python3.7/gzip.py", line 463, in read if not self._read_gzip_header(): File "/Users/chengxuan/anaconda3/lib/python3.7/gzip.py", line 411, in _read_gzip_header raise OSError('Not a gzipped file (%r)' % magic) OSError: Not a gzipped file (b'\x03\xeb') gzip报错啦,这个怎么处理?

asyncins commented 5 years ago

Traceback (most recent call last): File "huobi-asyncio-ws.py", line 33, in asyncio.get_event_loop().run_until_complete(startup(remote)) File "/Users/chengxuan/anaconda3/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete return future.result() File "huobi-asyncio-ws.py", line 17, in startup result=gzip.decompress(msg).decode('utf-8') File "/Users/chengxuan/anaconda3/lib/python3.7/gzip.py", line 532, in decompress return f.read() File "/Users/chengxuan/anaconda3/lib/python3.7/gzip.py", line 276, in read return self._buffer.read(size) File "/Users/chengxuan/anaconda3/lib/python3.7/gzip.py", line 463, in read if not self._read_gzip_header(): File "/Users/chengxuan/anaconda3/lib/python3.7/gzip.py", line 411, in _read_gzip_header raise OSError('Not a gzipped file (%r)' % magic) OSError: Not a gzipped file (b'\x03\xeb') gzip报错啦,这个怎么处理?

这个错误源自于 asyncio 的源码,错误类型是 OSError,要想找到根本原因,你可能需要断点进入到源码中。但看文件名 gzip.py,我猜应该跟你传输的消息有关,也许是 Frames。这里我也没能给出具体的解决办法,只能为你提供查找线索的思路