... Software for the people!
Become a member of Tropical Technologies Community, right now!
Search
Main Menu
Login
Username:

Password:

remember me

Lost Password?

Register now!
Recent Downloads
Shell Object Edito...
(Software, Mon, 18-May-2009)
TimeCube
(Software, Sat, 15-Nov-2008)
SciTE 1.76 plus Sh...
(Software, Sat, 05-Apr-2008)
Hotkey Plugin SDK
(QCD Plugins, Sun, 29-Oct-2006)
Hotkey Plugin
(QCD Plugins, Sun, 29-Oct-2006)
Transparent Image
(Samurize, Sat, 13-May-2006)
Lua Script (beta 6...
(Samurize, Fri, 03-Mar-2006)
Samurize Universal...
(Samurize, Tue, 24-Jan-2006)
Line Input Plugin
(QCD Plugins, Sun, 08-Jan-2006)
Custom Progress
(Samurize, Sun, 13-Mar-2005)
Internet stuff
Submitter: Tropics Date: 2006/2/24 0:06 Views: 206
Summary: This article shows two internet access scripts, one for downloading html files and one for MySql access.
Keywords: http ftp mysql

Downloading a file from the web
Luacurl is based on the libcurl library, which is extremely versatile and you can download and upload files in nearly every way possible.

init script
nothing of interest here, the same as usual
require "luacurl"


main script
result_data = ""
result_header = ""

function data(stream,buf)
    result_data = result_data .. buf
    return string.len(buf)
end

function header(stream,buf)
    result_header = result_header .. buf
    return string.len(buf)
end


---- HTTP GET test ------------------------------------------------------------------

c = curl.new() 
c:setopt(curl.OPT_URL,'http://www.lua.org')
c:setopt(curl.OPT_WRITEFUNCTION,data)
c:setopt(curl.OPT_HEADERFUNCTION,header)
--c:setopt(curl.OPT_PROXY,"localhost:3000")
--c:setopt(curl.OPT_PROXYUSERPWD,"ciccio:ciccio")
c:setopt(curl.OPT_HTTPHEADER,"User-Agent: curl (;;lua;;) luacurl")

code,err = c:perform()
if not code then return err end

result = "HEADER ("..string.len(result_header).." bytes):\n"..result_header
result = result.."DATA ("..string.len(result_data).." bytes):\n"..result_data

return result


The code defines two callback functions, which get called for chunks of incoming (downloaded) data. In this case, the data is just appended to a buffer. You could also write it to a file, if you want.

The curl stuff is set up then, by first creating a curl object (curl.new()). Curl objects do all the nasty stuff, and do only understand one command - "perform()". You tell them what exactly to do by using the setopt function. In this case, we want to download via http. The two callback functions get installed. The proxy options are there, too, if you need them.

If perform is successfull, it returns something different from nil. If an error occurs, it returns nil as the code and an error description as string in the err variable.
The rest is just concatenation of strings.
Documentation of the luacurl module can be found here.

The scripts need two of the standard libraries, the basic and the string library. The basic library for the "require" command, as always, and the string library contains the string.len function.

The scripts above are included with the plugin installer.
Pages: (1) 2 »
URL: http://www.tropictech.de/modules/article/view.article.php?c1/11
Rate
10987654321