원격 스크립트 실행 API
-
API 설명
POST /spawn HTTP/1.1
spawn_args: {"스크립트 실행 인수":...}
[스크립트 본문] -
가능한 응답
{"code":1,"message":"操作失败"}{"code":2,"message":"脚本有语法错误","detail":"구체적인 오류 정보"}{"code":3,"message":"已经有脚本正在运行中"}{"code":233,"message":"未知错误"} -
설명
- [스크립트 실행 인수]는 줄 바꿈 문자가 없는 출력 가능한 문자열입니다.
- [스크립트 본문]은 스크립트 내용이며, 텍스트 또는 암호화된 바이너리 데이터일 수 있습니다.
- 스크립트에서는 proc_get 또는 proc_take를 통해 스크립트 실행 인수를 가져올 수 있습니다.
- 스크립트는 장치에 파일로 저장되지 않고 실행만 됩니다.
- 단, 장치에서 스크립트 데몬 모드를 활성화한 경우에는 장치와 관련된 스크립트 데이터를 캐시합니다.
-
예제
예를 들어 다음 요청을 구성해 장치로 전송할 수 있습니다.
POST /spawn HTTP/1.1
spawn_args: {"server_ip":"192.168.31.13","port":55555}
Content-Length: 123
local args = proc_get("spawn_args")
args = json.decode(args)
sys.alert("帐号服务器的 IP 为:"..args.server_ip.."\n 端口为:"..args.port)장치에 다음 알림 창이 표시됩니다 ->
계정 서버 IP: 192.168.31.13
포트: 55555- 예제의 Python 구현(Python 3.x용):
# -*- coding: utf-8 -*-
import http.client
import json
spawn_args = {
"server_ip": "192.168.31.13", # 컴퓨터 측 IP 주소
"port": 55555, # 컴퓨터 측 포트
}
data = r'''
local args = proc_get("spawn_args")
args = json.decode(args)
sys.alert("帐号服务器的 IP 为:"..args.server_ip.."\n 端口为:"..args.port)
'''
headers = {
"Content-type": "text/lua",
"spawn_args": json.dumps(spawn_args),
}
conn = http.client.HTTPConnection("192.168.31.72:46952") # 장치 측 주소 및 포트
conn.request("POST", "/spawn", data.encode('utf-8'), headers) # data를 UTF-8로 인코딩
response = conn.getresponse()
print(response.status, response.reason) # print() 함수 사용
data = response.read()
print(data.decode('utf-8')) # 응답 데이터를 디코딩한 후 print() 함수 사용
conn.close()