在OpenWrt中利用nixio完成socket通信

在OpenWrt中使用socket通信主要就是调用nixio.socket来完成。我们可以写一个模块,然后在需要使用的时候使用require来引入。

说明:lua是脚本语言,可在htm文件内直接使用<%%>进行引用。
阅读代码,其中luci.http是在/usr/lib/lua/luci目录下安装自带的http.lua文件;luci.resttemplate是我自定义的根据nixio编写的socket通讯简单工具类,能访问远程的服务,调用远程的接口!
同理,在/usr/lib/lua/luci目录下创建resttemplate.lua文件,内容如下:

复制代码
--resttemplate.lua 
core={}

local h=require "luci.http"
local n=require "nixio"

core.ip="192.168.80.192"
core.port=8080
core.uri="/users"
core.method="GET"
core.charset="UTF-8"
core.rcvTimeout=20

function core.get(name)

end

function core.httpData(data)
--    core.uri=string.upper(core.uri)
    core.uri="/users"
    return core.method.." "..core.uri.." HTTP/1.1\r\n"..
        "Host: "..core.ip..":"..core.port.."\r\n"..
        "Content-type: text/html;charset="..core.charset.."\r\n"..
        "Accept-Language: zh-cn\r\n"..
        "User-Agent: Mozilla/4.0(Compatible win32; MSIE)\r\n"..
        "Content-Length: "..string.len(data).."\r\n"..    
        "Connection: close\r\n\r\n"..
        data
end

function core.restData(method,uri,data,ip,port)
    data=data or ""
    method=method or "GET"
    uri=uri or "/"
    return method.." "..uri.." HTTP/1.1\r\n"..
        "Accept: */*\r\n"..
        "Accept-Encoding: gzip, deflate, br\r\n"..
        "Accept-Language: zh-CN,zh;q=0.9\r\n"..
        "Connection: keep-alive\r\n"..
        "Host: "..ip..":"..port.."\r\n"..
        "Content-Length: "..string.len(data).."\r\n"..
        data
end

function core.testget(data)
        
    socket:setopt("socket","rcvtimeo",timeout)
    socket:send(core.httpData(data))
    local tt={}
    repeat
        local tmp=socket:recv(100)
        if tmp==false then
            socket:close()
            return false,"response timeout"
        end
        
        tmp=tostring(tmp)
        local i= #tt
        tt[i+1]=tmp
    until #tmp < 1
    
    socket:close()

    return true,"success 200"
end

function core.send(data)
    local position
    local t={}
    local http
    local socket=nixio.socket("inet","stream")
    local tmp

    if not socket then
        return false, "创建socket失败"
    end

    if not socket:connect(core.ip,core.port) then
        socket:close()
        return false, "服务无响应,连接服务器"..core.ip..":"..core.port.."失败"
    end

    socket:setopt("socket","rcvtimeo",core.rcvTimeout)

    socket:send(core.httpData(data))

    repeat
        tmp=socket:recv(100)
    
        if tmp==false then
            socket:close()
            return false,"响应超时"
        end
        tmp=tostring(tmp)
        t[#t + 1] = tmp
    until #tmp < 1

    socket:close()
    
    local result=table.concat(t)
    
    position=string.find(result,"\r\n\r\n")

    if position==nil then
        return false,"返回的数据格式不合法。数据:"..result
    end

    result=string.sub(result,string.find(result,"\r\n\r\n")+4)

    return result
end

--create a socket with ip,port,timeout
function core.socket(ip,port,timeout)
    local socket=nixio.socket("inet","stream")
    if not socket then
        return false,"create socket('inet','stream') failed!"
    end
    if not socket:connect(ip,port) then
        socket:close()
        return false,"connect "..ip..":"..port.." failed!"
    end
    socket:setopt("socket","rcvtimeo",timeout)
    return socket
end

function core.post()

end

return core
复制代码

 

使用示例:

复制代码
    local h=require "luci.http"
    local c=require "luci.resttemplate"
    

--    h.write("Hello World</br>")
--    h.write("测试socket通信失败!<Br>")
--    h.write(c.send("luci web's page Client!"))

    h.write(c.send(""))
复制代码

https://blog.csdn.net/qq_41953807/article/details/106229876

 

posted @   笠航  阅读(2034)  评论(1编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示