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),
}
logDebug(res)
logDebug(code)
if type(response_body) == "table" then
logDebug(table.concat(response_body))
else
logDebug("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")
local file = io.open("要保存的路径","w")
file:write(ret)
file:close()
end
示例
function main()
local ftp = require("socket.ftp")
ret = ftp.get("ftp://evan:123456@www.touchelf.com/evan.lua;type=i")
local file = io.open("/var/touchelf/scripts/evan.lua","w")
file:write(ret)
file:close()
end
FTP连接问题使用此函数检测错误
function main()
local ftp = require("socket.ftp")
ret,err = ftp.get("ftp://evan:123456@www.touchelf.com/evan.lua;type=i")
logDebug(err)
end
ftpPut
function main()
local ftp = require("socket.ftp")
local file = io.open("要上传文件的路径", "rb")
local data = file:read("*all")
file:close();
ret, err = ftp.put("ftp://用户名:密码@ftp地址/文件;type=i", data)
logDebug(ret)
logDebug(err)
end
示例
function main()
local ftp = require("socket.ftp")
local files = io.open("/var/touchelf/x.bmp", "rb")
local data = files:read("*all")
files:close();
ret, err = ftp.put("ftp://evan:123456@www.touchelf.com/x.bmp;type=i", data)
logDebug(ret)
logDebug(err)
end