• 在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
    "..
            "Host: "..core.ip..":"..core.port.."
    "..
            "Content-type: text/html;charset="..core.charset.."
    "..
            "Accept-Language: zh-cn
    "..
            "User-Agent: Mozilla/4.0(Compatible win32; MSIE)
    "..
            "Content-Length: "..string.len(data).."
    "..    
            "Connection: close
    
    "..
            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
    "..
            "Accept: */*
    "..
            "Accept-Encoding: gzip, deflate, br
    "..
            "Accept-Language: zh-CN,zh;q=0.9
    "..
            "Connection: keep-alive
    "..
            "Host: "..ip..":"..port.."
    "..
            "Content-Length: "..string.len(data).."
    "..
            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,"
    
    ")
    
        if position==nil then
            return false,"返回的数据格式不合法。数据:"..result
        end
    
        result=string.sub(result,string.find(result,"
    
    ")+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

  • 相关阅读:
    2019 SDN上机第5次作业
    hdu 2553 N皇后问题(递归)
    百练oj 2766 最大子矩阵和
    POJ 1664 放苹果
    POJ 3617 Best Cow Line(贪心)
    HDU 2013 ACM/ICPC Asia Regional Hangzhou Online ------ Zhuge Liang's Mines
    HDU 4712 Hamming Distance (随机算法)
    HDU 1171 Big Event in HDU
    HDU 1085 Holding Bin-Laden Captive!
    HDU 1028 母函数
  • 原文地址:https://www.cnblogs.com/7qin/p/13510443.html
Copyright © 2020-2023  润新知