url请求
--[[local g = require 'library.global'
--__ml_ss = mlc.prefix.ml_psession..tostring(os.time());
local mlc = require 'ml_config'
local function haslogin()
local r = g.getredis()
local s,e = r:get();
if s ~= nil then
return true;
else
return false;
end
end
local ffiok, ffi = pcall(require,"ffi")
--require('library.kav_fnc')
--md5 = require('library.md5')
system_hid = {}
--require "redis"
--[[local myclient = redis.connect();
local all_keys = myclient:keys("*");
local all_values = myclient:mget(all_keys);
for i, k in ipairs( all_keys) do
local v = all_values[i];
print(k, v)
end --]]
--[[local result ={}
for i = 1,#(KEYS) do
result[i]= redis.call('get',KEYS[i])
end
return result--]]
--[[local result={};
local myperson=KEYS[1];
local nums=ARGV[1];
local myresult =redis.call("hkeys",myperson);
for i,v in ipairs(myresult) do
local hval= redis.call("hget",myperson,v);
redis.log(redis.LOG_WARNING,hval);
if(tonumber(hval)<tonumber(nums)) then
table.insert(result,1,v);
end
end
fodr i=1,#(result) do
print(result[i])
end
return result;--]]
--]]
--[[local myKeyList=redis.call("keys","string.tmd.*")
local count=0
local tempStrlocal str --experience all the keys 遍历数组 有多种方法遍历数组 网上很容易找到
for index,line in pairs(myKeyList) do --通过Redis API调用 获取line的值 line即为键
str=redis.call("get",line) --string.sub() match() gmatch() len() 对字符串操作的函数
local matchStr=string.match(str,"http://tmd2.ghost.com/comment/images/%d+.png")
--匹配到http链接 --下面注释掉的函数可以在"服务器"端打印变量的值
print(matchStr)
local reserveStr=matchStr.sub(matchStr,40,string.len(matchStr));
--将链接截断,并替换成指定的模式
--print(reserveStr) -- .. 用于连接字符串 不像java中用 + 此处替换 str为源 中间为模式 右边为目标
tempStr=string.gsub(str,"http://tmd2.ghost.com/comment/images/%d+.png","http://tmd2-img.ghost.com/1/"..reserveStr) --print(tempStr) --替换完后将之重新设置到Redis中
redis.call("set",line,tempStr)
print(redis.call("get",line))
end--]]
---------------------------------------------
二、lua发送http请求代码
1.get请求
bash
local zhttp = require "resty.http"
local function http_post_client(url, timeout)
local httpc = zhttp.new()
timeout = timeout or 30000
httpc:set_timeout(timeout)
local res, err_ = httpc:request_uri(url, {
method = "GET",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
}
})
httpc:set_keepalive(5000, 100)
--httpc:close()
return res, err_
end
2.post请求
bash
local zhttp = require "resty.http"
local function http_post_client(url,body,timeout)
local httpc = zhttp.new()
timeout = timeout or 30000
httpc:set_timeout(timeout)
local res, err_ = httpc:request_uri(url, {
method = "POST",
body = body,
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
}
})
httpc:set_keepalive(5000, 100)
httpc:close()
if not res then
return nil, err_
else if res.status == 200 then
return res.body, err_
else
return nil, err_ end
end
end
bash
--get
local resp, err = http_post_client("http://zixuephp.net/index.html?name=test",3000)
--post
local body = {"name" = "test"}
local resp, err = http_post_client("http://zixuephp.net/index.html?name=test",body,3000)
----------------------------------------
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")
------------------------------------------------------------------------------------------------------------------
local function postdata(url,postdata)
local res="";
c = curl.new()
c:setopt(curl.OPT_URL,url)
c:setopt(curl.OPT_POSTFIELDS,postdata)
c:setopt(curl.OPT_WRITEFUNCTION, function(userparam,buffer)
if buffer ~= nil then
--local f = io.open(kav_fnc.getmodulepath()..'session2.dat','w')
--if f ~= nil then
--f:write(buffer);
print( buffer);
--f:close();
--end
res =res .. buffer;
return #buffer;
end
end)
local i,j,k = c:perform();
local rep=string.char(239, 187, 191);-- 防 utf标志开头 EF BB BF
res =string.gsub(res,rep,'');
if i == nil then
return nil,j,k;
else
return res;
end
return nil;
end
在C:\Users\Administrator\Desktop\lua\library\util.lua
local M = {}
_G[NAME] = M
function M.getFile(file_name)
local f = assert(io.open(file_name, 'r'))
local string = f:read("*all")
f:close()
return string
end