lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Because I got a few requests by mail a few examples:

File operations:
local nixio = require "nixio", require "nixio.util"
local file = nixio.open("myfile", "w")		-- Open myfile for writing
file:writeall("somedata")				-- Write some buffer into a file
file:seek(5)						-- Seek to offset 5
file:lock("lock", 2)					-- Lock 2 bytes beginning from current offset
file:close()

FS operations:
local fs = require "nixio.fs"
for entry in fs.dir("/tmp") do print(entry) end	-- Traverse directory
fs.copyr("/tmp", "/tmp/tmp2")				-- Recursive copying

Hashing:
local nixio = require "nixio", require "nixio.util"
local newmd5 = nixio.crypto.hash("md5")
newmd5:update("Lorem ipsum")
print((newmd5:update("some stuff"):final()))

Sockets:
local nixio = require "nixio", require "nixio.util"
-- local mysock = nixio.connect("ipv6.google.com", 443)	-- IPv6 is supported
local mysock = nixio.connect("google.com", 443)	-- Open TCP connection
print(mysock:getpeername())
local mytlssock = nixio.tls("client"):create(mysock)	-- TLS context and socket
mytlssock:connect()							-- TLS Handshake
mytlssock:writeall("GET / HTTP/1.0\r\n\r\n")		-- High-level write
print(mytlssock:read(1024))
mytlssock:close()