#!/usr/bin/env python3 # -*- coding: utf-8 -*- r'''单独查 set_code 为什么没返回 kicked。''' import json import time import urllib.error import urllib.request BASE = "http://127.0.0.1:8080" KEY = open("/root/lanremote-relay/data/admin_key.txt", encoding="utf-8").read().strip() def call(path, payload=None, method="POST", admin=False): data = json.dumps(payload).encode() if payload is not None else None h = {"Content-Type": "application/json"} if admin: h["X-Admin"] = KEY req = urllib.request.Request(BASE + path, data=data, headers=h, method=method) try: with urllib.request.urlopen(req, timeout=15) as r: body = r.read().decode("utf-8", "replace") try: return r.status, json.loads(body) except Exception: return r.status, body except urllib.error.HTTPError as e: body = e.read().decode("utf-8", "replace") try: return e.code, json.loads(body) except Exception: return e.code, body stamp = int(time.time()) dev = "dbg-%d" % stamp c1 = "%06d" % ((stamp + 1) % 1000000) c2 = "%06d" % ((stamp + 2) % 1000000) print("1. 注册设备") print(" ", call("/v1/agent/register", {"deviceId": dev, "code": c1, "name": "dbg", "version": "1.8.2"})) print("2. attach 造一个会话") print(" ", call("/v1/client/attach", {"code": c1, "name": "手表"})) print("3. 看会话数") st, h = call("/v1/health", method="GET") print(" sessions =", h.get("sessions")) print("4. 改码 %s -> %s" % (c1, c2)) st, r = call("/v1/admin/set_code", {"deviceId": dev, "code": c2}, admin=True) print(" 状态码:", st) print(" 响应:", json.dumps(r, ensure_ascii=False)) print("5. 再改一次(同样改 %s -> 另一个码)" % c2) c3 = "%06d" % ((stamp + 3) % 1000000) st, r = call("/v1/admin/set_code", {"deviceId": dev, "code": c3}, admin=True) print(" 响应:", json.dumps(r, ensure_ascii=False)) print("6. 清理") print(" ", call("/v1/admin/delete", {"deviceId": dev}, admin=True))