Lua sockets 05-27-2013, 05:04 PM
#1
Lua sockets
Lua's HTTP and TCP sockets.
Basic knowledge of general computer programming, sockets and Lua is required to completely understand the content of this page.
HTTP sockets
For the first way to handle an HTTP socket in Lua we'll use the "request" function. The request function can be used in a string-based simple form to download a URL using either the POST or the GET method or in a LTN12-based generic form capable of using any HTTP method.
These are the constants that control the HTTP module:
In the above example, http (in http.request) is a variable:
This is an example usage of http.request:
The above script will download test.txt's data, output it and then discard it.
If you wanted to save the downloaded data to a local file (let's say file.txt), you could use:
Another way to retrieve data using the HTTP protocol could be:
TCP sockets
This is a code to handle incoming data through the TCP protocol:
Sources: http://w3.impa.br/~diego/software/luasocket/http.html
Special thanks to: @JesseH, @asiktumar.
Lua's HTTP and TCP sockets.
Basic knowledge of general computer programming, sockets and Lua is required to completely understand the content of this page.
HTTP sockets
For the first way to handle an HTTP socket in Lua we'll use the "request" function. The request function can be used in a string-based simple form to download a URL using either the POST or the GET method or in a LTN12-based generic form capable of using any HTTP method.
These are the constants that control the HTTP module:
http://w3.impa.br/~diego/software/luasocket/http.html Wrote:PORT: default port used for connections;
PROXY: default proxy used for connections;
TIMEOUT: sets the timeout for all I/O operations;
USERAGENT: default user agent reported to server.
http.request(url [, body]) -- simple form
http.request{ -- generic form
url = string, -- requested URL
[sink = LTN12 sink,] -- if sink is nil the downloaded data is discarded
[method = string,] -- e.g: HEAD, POST; default is GET
[headers = header-table,] -- additional HTTP headers
[source = LTN12 source], -- LTN12 source to provide a request body; if there is a body, you need a provide a content-lenght
[step = LTN12 pump step,] -- LTN12 pump step function used to move data
[proxy = string,] -- URL of the proxy
[redirect = boolean,] -- follow redirects (true/false)
[create = function] -- optional function to substitue default socket.tcp when communications socket is created
}
In the above example, http (in http.request) is a variable:
Code:
http = require "socket.http"This is an example usage of http.request:
Code:
local http = require("socket.http")
local ltn12 = require("ltn12")
http.request{
url = "http://www.server.com/text.txt",
sink = ltn12.sink.file(io.stdout)
}The above script will download test.txt's data, output it and then discard it.
If you wanted to save the downloaded data to a local file (let's say file.txt), you could use:
Code:
sink = ltn12.sink.file(io.open("file.txt","w"))Another way to retrieve data using the HTTP protocol could be:
Code:
require("socket")
s = socket.connect(host,port)
s:send("GET / HTTP/1.0\r\n\r\n")
while true do
s, status, partial = client:receive(1024)
print (s or partial)
if status == "closed" then break end
end
s:close()TCP sockets
Code:
socket = require("socket")
s = socket.tcp()
assert(s:connect(host, port), "Error! Connection failed!") -- attempts to connect to the server. in case of failure, it returns the error message.
-- Example:
-- assert(s:connect("www.google.com",80))
s:settimeout(0) -- set the timeout for the "s" object to 0 to avoid freezing.
s:send("This data is sent to the host through the port.\r\n")
s:close()This is a code to handle incoming data through the TCP protocol:
Code:
s = assert(socket.bind(host, port))
i, p = s:getsockname()
assert(i, p)
print("Listening...")
data = assert(s:accept())
print("Connection established, now printing received data...")
rcv, endd = data:receive()
while not endd do
print(rcv)
rcv, endd = data:receive()
end
print(endd)Sources: http://w3.impa.br/~diego/software/luasocket/http.html
Special thanks to: @JesseH, @asiktumar.
My Bitcoin address: 1AtxVsSSG2Z8JfjNy9KNFDUN6haeKr7LiP
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U
If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U
If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize


![[+]](https://sinister.li/images/modern/collapse_collapsed.png)