Lua访问网页
Posted gd-luojialin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lua访问网页相关的知识,希望对你有一定的参考价值。
示例
例子,实现https方式,登录网站,访问某个网页,修改其中参数的功能。其中xx应用时候需要修改。
require("curl")
local ipList =
"192.168.1.1",
"192.168.1.1",
--登陆
function loginWeb(ip)
c = curl.easy_init()
c:setopt(curl.OPT_SSL_VERIFYHOST, 0);
c:setopt(curl.OPT_SSL_VERIFYPEER, 0);
c:setopt(curl.OPT_URL, "https://"..ip.."/")
c:setopt(curl.OPT_POSTFIELDS, "Username=admin&Password=admin&Frm_Logintoken=&action=login")
c:setopt(curl.OPT_WRITEFUNCTION, function(buffer)
--print(buffer)
--print("\r\n---------------------------\r\n");
return #buffer
end)
c:perform()
end
--访问页面
function accessPage(ip)
c = curl.easy_init()
c:setopt(curl.OPT_SSL_VERIFYHOST, 0);
c:setopt(curl.OPT_SSL_VERIFYPEER, 0);
c:setopt(curl.OPT_URL, "https://"..ip.."/xxpage.html")
c:setopt(curl.OPT_WRITEFUNCTION, function(buffer)
--print(buffer)
--print("\r\n---------------------------\r\n");
return #buffer
end)
c:perform()
end
--设置参数
function setParameter(ip, file)
c = curl.easy_init()
c:setopt(curl.OPT_SSL_VERIFYHOST, 0);
c:setopt(curl.OPT_SSL_VERIFYPEER, 0);
c:setopt(curl.OPT_URL, "https://"..ip.."/xx.php")
c:setopt(curl.OPT_POSTFIELDS, "DaylightSavingsUsed=1&Dscp=-1")
local htmlTable =
c:setopt(curl.OPT_WRITEFUNCTION, function(buffer)
--print(buffer)
--print("\r\n---------------------------\r\n");
table.insert(htmlTable, buffer)
return #buffer
end)
c:perform()
local htmlStr = table.concat(htmlTable);
local resultBuff = "";
if string.match(htmlStr, "<result>SUCC</result>") ~= nil then
resultBuff = ip.." config OK\r\n";
print(resultBuff)
file:write(resultBuff);
else
resultBuff = ip.." config NOK\r\n";
print(resultBuff)
file:write(resultBuff);
end
end
local file = io.open(".\\result.txt", "w+");
for key,ip in ipairs(ipList) do
loginWeb(ip);
accessPage(ip);
openLightSave(ip, file);
end
file:close();
print("Done")
以上是关于Lua访问网页的主要内容,如果未能解决你的问题,请参考以下文章