1. websockets报HTTP 401解决办法
代码如下:
#!/usr/bin/env python
import asyncio
import websockets
import requestsuri = 'ws://192.168.20.167/websocket'
msg = '''{"type":6,"param":{"businessType":3,"cmd":1,"frequency":200}}'''def login():log_data_dic = {"userName": 'admin', "password": 'admin', "rememberMe": 0}header = {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}login_url = 'http://192.168.20.167/login'session = requests.Session()response = session.post(login_url, data=log_data_dic, headers=header)print('response = ', response)async def test():login()async with websockets.connect(uri) as websocket:await websocket.send(msg)while True:response = await websocket.recv()print('response = ', response)asyncio.run(test())
执行后报如下错误:
解决办法:连接服务器时带上cookie,代码如下:
#!/usr/bin/env python
import asyncio
import websockets
import requestsdef login():log_data_dic = {"userName": 'admin', "password": 'admin', "rememberMe": 0}header = {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}login_url = 'http://192.168.20.167/login'session = requests.Session()response = session.post(login_url, data=log_data_dic, headers=header)cookie = response.cookiesprint('cookie = ', cookie)return cookieasync def test():cookie = login()uri = 'ws://192.168.20.167/websocket'msg = '''{"type":6,"param":{"businessType":3,"cmd":1,"frequency":200}}'''async with websockets.connect(uri, extra_headers={'Cookie':cookie}) as websocket:await websocket.send(msg)while True:response = await websocket.recv()print('response = ', response)asyncio.run(test())
运行结果:
2. websocket报HTTP 401解决办法
代码如下:
#!/usr/bin/env python
import asyncio
import websocket
import requestsuri = 'ws://192.168.20.167/websocket'
msg = '''{"type":6,"param":{"businessType":3,"cmd":1,"frequency":200}}'''def login():log_data_dic = {"userName": 'admin', "password": 'admin', "rememberMe": 0}header = {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}login_url = 'http://192.168.20.167/login'session = requests.Session()response = session.post(login_url, data=log_data_dic, headers=header)cookies = response.cookies.get_dict()access_token = cookies.get('access_token')cookie = f'access_token={access_token}'return cookieasync def test():cookie = login()cookie = f'{cookie}; userName=admin; role=ADMIN; isAuthenticated=true; devType=ue'ws = websocket.WebSocket()ws.connect(uri)ws.send(msg)while True:ret = ws.recv()print(ret)asyncio.run(test())
解决办法:连接服务器是带上cookie,代码如下:
#!/usr/bin/env python
import asyncio
import websocket
import requestsuri = 'ws://192.168.20.167/websocket'
msg = '''{"type":6,"param":{"businessType":3,"cmd":1,"frequency":200}}'''def login():log_data_dic = {"userName": 'admin', "password": 'admin', "rememberMe": 0}header = {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}login_url = 'http://192.168.20.167/login'session = requests.Session()response = session.post(login_url, data=log_data_dic, headers=header)cookies = response.cookies.get_dict()access_token = cookies.get('access_token')cookie = f'access_token={access_token}'return cookieasync def test():cookie = login()cookie = f'{cookie}; userName=admin; role=ADMIN; isAuthenticated=true; devType=ue'ws = websocket.WebSocket()ws.connect(uri, cookie=cookie)ws.send(msg)while True:ret = ws.recv()print(ret)asyncio.run(test())
运行结果如下: