Lua http socket evaluation

I use lua 5.1 and the luaSocket 2.0.2-4 to retrieve a page from a web server. I first check if the server is responding and then assign the web server response to lua variables.

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)

Everything works as expected but the request is executed two times. I wonder if I could do Something like (which doesn't work obviously):

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

Answers


Yes, something like this :

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

Request will be sent only once, and function will exit if it failed.


Need Your Help

Redesigning a site with fixed sizes

javascript html css

This turns out to be really interesting, I have got my question and replies in this site

How to force a .unityfile to text for Instance ID comparison using c#

c# unity3d

I am trying to force a .unity file, the scene files which unity uses to text. It is possible to change this in the editor for read out purposes by going into the edit > project settings > editor and

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.