Lua http socket评估

rip*_*pat 1 lua luasocket

我使用lua 5.1和luaSocket 2.0.2-4从Web服务器检索页面.我首先检查服务器是否正在响应,然后将Web服务器响应分配给lua变量.

local mysocket = require("socket.http")
if mysocket.request(URL) == nil then
    print('The server is unreachable on:\n'..URL)
    return
end
local response, httpCode, header = mysocket.request(URL)
Run Code Online (Sandbox Code Playgroud)

一切都按预期工作,但请求执行两次.我想知道我是否可以做某事(显然不起作用):

local mysocket = require("socket.http")
if (local response, httpCode, header = mysocket.request(URL)) == nil then
    print('The server is unreachable on:\n'..URL)
    return
end
Run Code Online (Sandbox Code Playgroud)

use*_*016 5

是的,像这样:

local mysocket = require("socket.http")
local response, httpCode, header = mysocket.request(URL)

if response == nil then
    print('The server is unreachable on:\n'..URL)
    return
end

-- here you do your stuff that's supposed to happen when request worked
Run Code Online (Sandbox Code Playgroud)

请求只会发送一次,如果失败,函数将退出.