Socket模块

httpGet

function main()
    local http = require("socket.http")
    http.TIMEOUT = 20 --设置超时时间
    ret = http.request("http://www.touchelf.com")
end

httpPost

function main()
    local http = require "socket.http"
    local ltn12 = require "ltn12"
    local request_body = [[login=user&password=123]] -- 这里是POST的数据
    local response_body = {}
    local res, code, response_headers = http.request{
        url = "http://127.0.0.1:8888", -- 这里是你提交的域名
        method = "POST",
        headers =
        {
            ["Content-Type"] = "application/x-www-form-urlencoded";
            ["Content-Length"] = #request_body;
        },
        source = ltn12.source.string(request_body),
        sink = ltn12.sink.table(response_body),
    }
    sys.log(res)
    sys.log(code)
    if type(response_body) == "table" then
        sys.log(table.concat(response_body))
    else
        sys.log("Not a table:", type(response_body))
    end
end

ftpGet

function main()
    local ftp = require("socket.ftp")
    ftp.TIMEOUT = 20 --设置超时时间
    ret = ftp.get("ftp://用户名:密码@ftp地址/文件;type=i")
    file.write("要保存的路径",ret)
end

示例

function main()
    local ftp = require("socket.ftp")
    ret = ftp.get("ftp://evan:123456@www.touchelf.com/evan.lua;type=i")
    file.write("/var/touchelf/scripts/evan.lua",ret)
end

FTP连接问题使用此函数检测错误

function main()
    local ftp = require("socket.ftp")
    ret,err = ftp.get("ftp://evan:123456@www.touchelf.com/evan.lua;type=i")
    sys.log(err)
end

ftpPut

function main()
    local ftp = require("socket.ftp")
    local data = file.read("要上传文件的路径")
    ret, err = ftp.put("ftp://用户名:密码@ftp地址/文件;type=i", data)
    sys.log(ret)
    sys.log(err)
end

示例

function main()
    local ftp = require("socket.ftp")
    local data = file.read("/var/touchelf/x.bmp", "rb")
    ret, err = ftp.put("ftp://evan:123456@www.touchelf.com/x.bmp;type=i", data)
    sys.log(ret)
    sys.log(err)
end