lua环境搭建
1.打开终端执行以下命令安装最新版本brew
brew的作用是一个linux系统的应用类库中心,可以通过brew的指令集进行 查询 下载 安装 卸载
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 3.使用luarocks安装luasocket https://blog.csdn.net/fnzsjt/article/details/41803381?utm_source=blogxgwz5
luarocks install luasocket
4.lua学习笔记
为什么要用lua语言,因为最近做辅助的开发工具都支持lua脚本写的插件,还有它能垮平台,内存占用小。c语言直接开发的。运行速度也很快。
搭建完毕开发环境后我们只需要用 luarocks指令来查找安装我们需要的类库
mac打开终端:
luarocks search luasocket 查询库 luasocket
luarocks install luasocket 默认安装最新版本luasocket
luarocks search skjson 查询库 skjson
luarocks install skjson 默认安装最新版本skjson
5.lua http请求
1 local http=require("socket.http") 2 local ltn12 = require("ltn12") 3 local json = require ("dkjson") 4 local http=require("socket.http"); 5 local request_body = [[account=13983918072&deviceKey=fba71fd5ff7f4eb28c84a574b00c9989]] 6 local response_body = {} 7 8 local res, code, response_headers = http.request{ 9 url = "http://192.168.1.104:8080/autoDrainageServlet/AUserTokenVerify",10 method = "POST",11 headers =12 {13 ["Content-Type"] = "application/x-www-form-urlencoded";14 ["Content-Length"] = #request_body;15 },16 source = ltn12.source.string(request_body),17 sink = ltn12.sink.table(response_body),18 }19 20 print(res)21 print(code)22 23 if type(response_headers) == "table" then24 for k, v in pairs(response_headers) do25 print(k, v)26 end27 end28 29 print("Response body:")30 if type(response_body) == "table" then31 print(table.concat(response_body))32 else33 print("Not a table:", type(response_body))34 end35 --local tabledata = --table格式的数据--36 local jsondata = json.encode (response_body, { indent = true })37 print(jsondata)
打印结果
1200connection closecontent-type text/html;charset=utf-8transfer-encoding chunkeddate Sun, 30 Jun 2019 09:02:32 GMTResponse body:{ "pointAccount":"13983918072","Msg":"验证成功","historyFreezeTimes":"0","deviceKey":"fba71fd5ff7f4eb28c84a574b00c9989","Success":"1","useStatus":"离线","token":"d1451f5f430d49d3b055596e5859316b","createData":"2019-06-19 08:48:23.0","freeze":"未冻结","dianzan":"否","addManyTimesChangeQQ":"6","id":"168","todayVerifyFailTimes":"0"}["{\"pointAccount\":\"13983918072\",\"Msg\":\"验证成功\",\"historyFreezeTimes\":\"0\",\"deviceKey\":\"fba71fd5ff7f4eb28c84a574b00c9989\",\"Success\":\"1\",\"useStatus\":\"离线\",\"token\":\"d1451f5f430d49d3b055596e5859316b\",\"createData\":\"2019-06-19 08:48:23.0\",\"freeze\":\"未冻结\",\"dianzan\":\"否\",\"addManyTimesChangeQQ\":\"6\",\"id\":\"168\",\"todayVerifyFailTimes\":\"0\"}\n"]