Code Snippets

Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> http = require("socket.http")
> content, statuscode, response_header = http.request("http://somedomain.com/download.php", "a=1")
> print(statuscode)
200
> for key, value in pairs(response_header) do
>> print(key .. ":\t" .. value)
>> end
connection:		close
content-type:	text/html; charset=UTF-8
date:			Wed, 11 Apr 2018 14:30:11 GMT
x-powered-by:	PHP/7.2.3
transfer-encoding:	chunked
server:			Apache/2.4.29 (Win32) OpenSSL/1.1.0g PHP/7.2.3
>
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> host, port = "http://somedomain.com", 123
> socket = require("socket")
> tcp = assert(socket.tcp())
> tcp:connect(host, port);
> tcp:send("Hello world\n");		--note the newline
> while true do
>> local s, status, partial = tcp:receive()
>> print(s or partial)
>> if status == "closed" then break end
>> end
> tcp:close()


References

  1. http://w3.impa.br/~diego/software/luasocket/http.html
  2. http://w3.impa.br/~diego/software/luasocket/tcp.html
  3. https://stackoverflow.com/questions/9013290/lua-socket-client