This its a 'simple' example to use luasocket ^^
in this case you can download files using lua
^using
param byte its for change maximun size of bytes on each receive.
delay its an delay on each recive
and HTMLver set to '1.0' or '1.1' some webserver dont support 1.1 or 1.0
in this case you can download files using lua
Lua:
function downloadFile(IP,PORT,fileRequested,outputName, byte_s ,delay,htmlVer)
--- Function by mock
assert(fileRequested,'fileRequested is nil!')
IP = IP or 'localhost'
PORT = PORT or 80
local client = socket.connect(IP,PORT)
print('starting download in: http//'..IP..':'..PORT..'/'..fileRequested..'/')
if client then
client:setoption('keepalive', true)
client:send('GET /'..fileRequested..' HTTP/'..(htmlVer or '1.0')..'\r\n\r\n')
local byte = 0
local findfile = false
local star = os.clock()
local file = io.open(outputName, "wb")
if not file then
print('Cannot create '..fileRequested)
return false
end
repeat
r = client:receive(sizee)
if r then
if string.find(r,'Content%-Length:') then
size = string.match(r,[[Content%-Length: (%d+)]]) or '??'
print('Size:'..size)
end
end
if r == "" then
findfile = true
print('Starting')
end
if findfile == true and r then
file:write(r)
byte = byte+r:len()
sizee = byte_s or 1048
os.sleep(delay or 0.001)
per = math.percent(byte, size or 100,1)
if per ~= old then
print(byte..'/'..(size or '?'),per..'%')
old = per
end
end
until not r
file:close()
print('\aDownload finished in '..(os.clock()-star)..' seconds.')
else
print('Server is off?')
return false
end
return true
end
downloadFile('img6.imageshack.us',80,'img6/7580/aaanz5.jpg','q.jpg')
Code:
downloadFile('img6.imageshack.us',80,'img6/7580/aaanz5.jpg','q.jpg')
Code:
downloadFile(IP,PORT,fileRequested,outputname[I][,bytes,delay,htmlVer][/I])
param byte its for change maximun size of bytes on each receive.
delay its an delay on each recive
and HTMLver set to '1.0' or '1.1' some webserver dont support 1.1 or 1.0