Authentication
认证方式
python实现
import requests
from requests.auth import HTTPDigestAuth
from ws4py.client.threadedclient import WebSocketClient
import json
import xmltodict
def xml_to_json(xml_str):
xml_parse = xmltodict.parse(xml_str)
json_str = json.dumps(xml_parse, indent=1)
return json_str
class EchoClient(WebSocketClient):
def opened(self):
print("Web socket连接已建立")
def closed(self, code, reason):
print(("Closed down", code, reason))
def received_message(self, m):
print("=> %d %s" % (len(m), str(m)))
digest_auth = HTTPDigestAuth('Default User', 'robotics')
content_header = {'Content-Type': 'application/x-www-form-urlencoded;v=2.0'}
subscription_url = 'http://127.0.0.1:80/subscription?json=1'
payload = {'resources': ['1', '2'],
'1': '/rw/iosystem/signals/Local/DRV_1/MOTLMP;state',
'1-p': '1',
'2': '/rw/panel/opmode',
'2-p': '1'
}
session = requests.Session()
resp = session.post(subscription_url, auth=digest_auth, headers=content_header, data=payload, verify=False)
if resp.status_code == 201:
print(resp.text)
location = resp.headers['Location']
print(resp.cookies['-http-session-'])
print(resp.cookies['ABBCX'])
cookie = '-http-session-={0}; ABBCX={1}'.format(resp.cookies['-http-session-'], resp.cookies['ABBCX'])
header = [('Cookie', cookie)]
print(" resp header :", header)
print("ws 地址:", location)
'''
Cookie: -http-session-=15::http.session::3a7164f8f3668128db7724dc5a680d2c; ABBCX=24
'''
ws = EchoClient(location , protocols=['robapi2_subscription'], headers=header)
ws.connect()
ws.run_forever()
闭坑
digest_auth = HTTPDigestAuth('Default User', 'robotics')
ws = EchoClient(location , protocols=['robapi2_subscription'], headers=header)
Postman

Subscriptions
webSocket
websocket = new WebSocket('ws://127.0.0.1:80', ['robapi2_subscription']);
websocket.addEventListener('open', function (event) {
console.log('ws 连接...');
});
websocket.addEventListener('message', function (event) {
console.log('服务器消息: ', event.data);
});