|
| LuaSocket |
| IPv4 Sockets support for the Lua language |
LuaSocket is a Lua extension library that provides support for the TCP and UDP transport layers within the Lua language. The library also provides support for SMTP (sending e-mails), HTTP (www access) and FTP (uploading and downloading files).
LuaSocket can be used by any Lua application desiring access to network communication on the Internet, once it has been properly linked with and initialized by the interpreter running the Lua application. The code has been tested and runs well on several Windows and Unix platforms.
The library is available under the same terms and conditions as the Lua language, that is, it can be used at no cost for both academic and commercial purposes.
Copyright © 1999-2001 TeCGraf, PUC-Rio. All rights reserved.
Author: Diego Nehab
All previous versions of the LuaSocket library can be downloaded here. Although these versions are no longer supported, they are still available for those that have compatibility issues.
LuaSocket version 1.3b is now available for download! It is compatible with Lua 4.0 and has been tested on Windows 98, Windows 2000, Linux, AIX, SunOS and IRIX.
The major improvements over previous versions are in the HTTP and FTP support. Users have the ability to download and upload information chunk by chunk, so that the amount of memory spent in transfers can be bounded. There have also been some bug-fixes:
There should be no incompatibilities.
The library can be downloaded in source code from the following links:
luasocket-1.3b.tar.gz
luasocket-1.3b.zip
Besides the full C and Lua source code for the library, the distribution contains several examples, this user's manual and the test procedures.
Below is a little introduction on how to get the library up and running with your applications and a little introduction on network programming with LuaSocket.
To have the library functions made available to a Lua script, the interpreter running the script must be linked to the luasocket library, and to whatever libraries the OS in use requires for socket programming. The functions are registered in the Lua state given as the parameter to the function lua_socketlibopen, the only C function exported by the library. The scripts can then use all registered functions.
The network support in the Lua language could closely mirror the C API or could implement a new, independent, transport layer abstraction. Having an API similar to the C API would make things easier for those who are used to socket programming. On the other hand, the simplicity of the Lua language would be lost. We ended up with something in between, in the sense that function names and semantics have been copied from the C API whenever possible, whereas their usage in Lua has been greatly simplified.
One of the major differences in the created API is the timeout control provided by the library. All I/O operations are blocking by default. In other words, the send, receive and accept functions will block the caller application until the operation is completed (if ever). The application can, however, specify upper limits on the time it can be blocked by LuaSocket ("return" timeout), on the time LuaSocket can be blocked by the OS ("blocked" timeout) or a combination of the two. Remember that each LuaSocket call performs several OS calls, so that the two timeout values are not equivalent.
Another major difference is the receive pattern capability. Applications can read data from a TCP client socket line by line, chunk by chunk, until the connection is closed etc. All I/O reads are buffered and the performance difference between different receive patterns is negligible.
Finally, host name resolution is transparent, meaning that most functions accept both IP addresses and host names. In case a host name is given, the library queries the system's resolver and tries all returned IP addresses until one succeeds or all fail. IP addresses are directly encoded and are therefore more efficient. The toip and tohostname functions are provided to convert between host names and IP addresses.
Socket objects are represented as tables in the Lua language. Different socket types accept different operations. These operations are available both as stand-alone global functions and as table methods of the socket objects (i.e. the calls send(sock, "test") and sock:send("test") are equivalent). It is considered better style to use the table method versions, which are also slightly more efficient.
TCP programming
TCP is a reliable stream protocol. In other words, applications send and
receive data as an error free stream of bytes. Data is split in one end
and reassembled transparently on the other end. There are no boundaries
in the data transfers. The library allows users to read data from the
sockets in several different granuralities: patterns are available for
words, lines, arbitrary sized blocks or "read up to connection closed",
all with similar performance.
The library distinguishes two types of TCP sockets: client sockets and
server sockets.
Client sockets are used to exchange data between two applications over
the Internet. Client sockets are created by the connect global
function or returned by the accept server socket method.
Applications can call the methods send and receive to
send and receive data. The other methods available for client socket
objects are getsockname, getpeername, timeout
and close.
Server sockets are created by the bind global function and are
associated with a address and port on the local host. Applications use
the accept server socket method to wait for a client connection
on a server socket. Once a connection is established, a client socket
object is returned representing this connection. The other methods
available for server socket objects are getsockname,
timeout and close.
UDP programming
UDP is a non-reliable datagram protocol. In other words, applications
send and receive data as independent blocks, which are not guaranteed to
reach the other end. Even when they reach the other end, they are not
guaranteed to be error free. Data transfers are atomic, one datagram at
a time. Reading only part of a datagram discards the rest, so that the
next read operation will act on the next datagram. The advantages are in
simplicity (no connection setup) and performance (no error checking or
error correction).
An UDP socket object is created by the udpcreate function. UDP
sockets do not need to be connected before use. The method
sendto can be used immediately after creation to send a
datagram to any UDP (ip, port) pair. Host names are not allowed for
performance reasons. Methods receive and receivefrom
can be used to retrieve datagrams, the latter returning the ip and port
of the sender as extra return values (thus being slightly less
efficient).
When communication is performed repeatedly with a single peer, an
application should call the setpeername method to specify a
permanent partner. The method send can then be used to send
data directly to the peer, and methods receive and
receivefrom will only return datagrams originating from that
peer. There is about 30% performance gain due to this practice.
To associate an UDP socket with a local address, an application calls
the setsockname function. Otherwise, the socket is
automatically bound to an ephemeral address before the first data
transmission. The other methods available for UDP sockets are
getpeername, getsockname, timeout and
close.
All the functions of the API are described below. Several examples are
given in the distribution, including the automated tests and the full
implementation of the protocols FTP, SMTP and HTTP.
Note that although some function names are overloaded, documentation is
provided separately for TCP and UDP sockets.
accept(socket)
Waits for a TCP client to attempt connection with the server
socket, and returns a client socket object connected to the
remote end. If a timeout condition is met, the function returns
nil followed by the string 'timeout'.
bind(address, port [, backlog])
Creates a new TCP server socket and binds it to (address,
port) on the local host. Address can be an IP address
or a host name. If address is '*', the system binds to
all local interfaces (INADDR_ANY). If port is 0, the
system automatically chooses an ephemeral port. The optional parameter
backlog (default value 1) specifies the number of client
connections that can be queued waiting for service. If the queue is full
and another client attempts connection, the connection is refused. In
case of success, the function returns a server socket, on which the
operations accept, close, getsockname and
listen are permitted. In case of error, the function returns
nil followed by a string describing the error.
close(socket)
Closes the socket socket. The local address to which
socket was bound is made available to other applications. No
further operations (except for further calls to close) are
allowed on a closed socket.
Note: It is important to close all used sockets once they are not
needed, since, in many systems, each socket uses a file descriptor,
which are a limited system resource.
connect(address, port)
Creates a new TCP client socket and tries to connect to
(address, port). Address can be an IP address
or a host name. In case of success, the function returns a client socket
on which the operations close, getsockname,
getpeername, receive, send and
timeout are permitted. In case of error, the function returns
nil followed by a string describing the error.
getpeername(socket)
Returns the IP address and port of the peer connected to the TCP client
socket or nil in case of error. Note that it makes no
sense to call getpeername on a server socket object.
getsockname(socket)
Returns the local IP address and port of the TCP socket
or nil in case of error.
receive(socket [, pattern1, pattern2, ... patternN])
Receives pattern1, pattern2, ...
patternN from the client socket. A pattern
can be one of the following:
Note: In case of error, the function always return everything it managed
to download before the error condition was met.
select(receive, send [, timeout])
Waits for a number of sockets to change status. Receive is a
table with the sockets to test for characters available for reading.
Sockets in the send table are watched to see if it is OK to
immediately write on them. Timeout is the maximum amount of
time (in seconds) to wait for a change in status. A nil,
negative or omitted timeout value allows the function to block
indefinitely. Receive and send can also be empty
tables or nil. Non-socket values in the tables will be silently
ignored (that way you can have a handy field named "n").
The function returns a table with the sockets ready for reading, a table
with the sockets ready for writing and an error message. The error
message is "timeout" if a timeout condition was met and nil
otherwise.
Important Note: a known bug in WinSock prevents select from
working properly on non-blocking TCP sockets. The function may return a
socket as writable even though the socket is not ready for
sending.
Note: calling select with a server socket in the
receive parameter before a call to accept does
not guarantee accept will return immediately. Use the
timeout server socket method or accept might
block forever.
Interesting note: as mentioned in some manuals, calling select
with both sets empty and a non-null timeout is a fairly portable way to
sleep with sub-second precision.
Examples:
Sends the strings string1,
string2, ... stringN through the
client socket socket. The function returns an error code, which
is nil in case of success, the string 'closed' in case
the connection was closed before the transmission was completed or the
string 'timeout' in case there was a timeout during the
operation. After the error code, the function returns the number of
bytes accepted by the transport layer.
timeout(socket, value [, mode])
Changes the timeout values for the socket socket. By default,
all I/O operations are blocking. That is, any call to the functions
send and receive will block indefinitely, until the
operation completes. The timeout function defines a limit on
the amount of time the functions can block, specified as the
value parameter, in seconds. There are two timeout modes and
both can be used together for fine tuning:
Note: although timeout values have millisecond precision, large blocks
can cause I/O functions not to respect timeout values due to the time
the library takes to transfer blocks to and from the kernel and to and
from the Lua interpreter.
close(socket)
Closes the socket socket. The local address to which it is
bound is made available to other applications. No further operations are
allowed on a closed socket.
Note: It is important to close all used sockets once they are not
needed, since, in many systems, each socket uses a file descriptor,
which are a limited system resource.
getpeername(socket)
Returns the IP address and port of the peer of the UDP socket.
The function will fail unless a peer has been set with a call to
setpeername.
getsockname(socket)
Returns the local IP address and port of the TCP socket or
nil in case of error.
Note: UDP sockets are not bound to any address until
setsockname or the sendto method is called for the
first time (in which case it is bound to an ephemeral port and the
wild-card address). The local address cannot be changed thereafter.
receive(socket [, number])
Receives a datagram from the UDP socket socket with up to
number bytes. If there are more than number bytes
available in the datagram, the remaining are discarded. If there are
less then number bytes available in the current datagram, the
available bytes are returned. If number is omitted, the maximum
datagram size is used. In case of timeout, the function returns
nil followed by the string 'timeout'. In case the
transmission failed, the function returns nil followed by the
string 'refused'.
receivefrom(socket [, number])
Works exactly as the receive function, except it returns the
sender ip and port as extra return values and
is therefore slightly less efficient.
select(receive, send [, timeout])
Waits for a number of sockets to change status. Receive is a
table with the sockets to test for characters available for reading. OK
Sockets in the send table are watched to see if it is to
immediately write on them. Timeout is the maximum amount of
time (in seconds) to wait for a change in status. A nil,
negative or omitted timeout value allows the function to block
indefinitely. Receive and send can also be empty
tables or nil. Non-socket values in the tables will be silently
ignored (that way you can have a handy field named "n").
The function returns a table with the sockets ready for reading, a table
with the sockets ready for writing and an error message. The error
message is "timeout" if a timeout condition was met and nil
otherwise.
send(socket, string)
Sends string to the UDP peer of socket socket. The
method setpeername MUST have been called on socket. If
successful, the function returns nil. In case of timeout, the
function returns the string 'timeout'. In case the transmission
failed, the function returns the string 'refused'.
sendto(socket, string, ip, port)
Sends string to (ip, port). In case of
timeout, the function returns the string 'timeout'. Ip
MUST be an IP address, for performance reasons. In case the transmission
failed, the function returns the string 'refused'.
setpeername(socket, address, port)
Sets the socket UDP peer to (address, port).
Address can be an IP address or a host name. After a call to
setpeername, the send and receive MUST be
used instead of sendto and receivefrom. Outgoing
datagrams will be sent to the specified peer, and datagrams received
from other peers will be discarded by the OS. Since the address of the
peer does not have to be passed to and from the OS, this practice is
recommended when the same peer is used for several transmissions and can
lead to up to 30% performance gains.
setsockname(socket, address, port)
Binds the UDP socket to a local address (address,
port). Address can be an IP address or a host name. If
address is '*' the system binds to all local
interfaces (INADDR_ANY). If port is 0, the system
chooses an ephemeral port. If successful, the function returns
nil. In case of error, the function returns an error message.
timeout(socket, value)
Changes the timeout value for the socket socket. By default,
all operations are blocking. The timeout function defines a
limit on the amount of time the functions can block, specified as the
value parameter, in seconds. A nil or negative timeout
value allows operations to block indefinitely.
Note: there is no send buffer on an UDP socket. Therefore, a send
operation on an UDP socket should never block, regardless of the timeout
value. Receive operations, however, can block the application.
udpsocket([options])
Creates and returns an UDP socket object, on which the methods
close, getpeername, getsockname,
receive, receivefrom, send, sendto,
setpeername, setsockname and timeout can be
used. In case of error, the function returns nil and an error
message.
The table options allows users to specify non-default socket
options at socket creation time. The table must be in the format:
The following functions can be used to convert between host names and IP
addresses. All information returned by the resolver is returned by these
functions, as a table in the form:
tohostname(address)
Returns a string with the canonic host name of given address,
followed by a table with all information returned by the resolver.
Address can be an IP address or host name. In case of error,
the function returns nil followed by an error message.
toip(address)
Returns a string with the first IP address found for address,
followed by a table with all information returned by the resolver.
Address can be an IP address or host name. In case of error,
the function returns nil followed by an error message.
Besides IPv4 transport layer support, the LuaSocket toolkit offers
straightforward support for the HTTP, SMTP and FTP protocols. The
support is implemented in the Lua language and is distributed in source
code as three separate modules.
The module smtp.lua provides functionality to send e-mail
messages to a SMTP mail server. The implementation conforms to
RFC 821.
MIME Headers are represented as a table in the form:
smtp_mail(from, rcpt, headers, body,
server)
Sends a message to recipient list rcpt, a lua table. The sender
is given by the e-mail address from. The message is composed by
the optional MIME Headers headers and text body. The
message is sent using the server server. If successful, the
function returns nil, otherwise an error message is returned.
Examples:
The mail function, for compatibility, implements the same interface
as that of CGILua 3.2,
except the mailserver parameter is mandatory.
The function returns nil if the message was sent successfully.
In case of error, an error message is returned.
HTTP and FTP transfers sometimes involve large amounts of information.
Sometimes an application needs to generate outgoing data in real time,
or needs to process incoming information as it is being received. To
address these problems, LuaSocket 1.3b allows HTTP message bodies and
FTP file contents can be received or sent through the callback mechanism
outlined below.
Instead of returning the whole contents of a FTP file or HTTP message
body as string to the Lua application, the library allows the user to
provide a receive callback that will be called with successive
chunks of data, as the data becomes available:
The callback provided by the user will be repeatedly called by the
library whenever new data is available. Each time it is called, the
callback receives successive chunks of downloaded data. When
the transmission is over, the function is called with an empty string
(i.e. "") as the chunk. If an error occurs, the
function receives a nil chunk and an error message as the
err argument. The callback can abort transmission by returning
nil as its first return value. In that case, it can also return
an error message. Any non-nil return value proceeds with the
transmission.
Examples:
The callback provided by the user will be repeatedly called whenever the
library needs more data to be sent. Each time the callback is called, it
should return the next part of the information the library is expecting,
followed by the total number of bytes to be sent. The callback can abort
the process at any time by returning nil followed by an
optional error message.
Obs: The need for the second return value comes from the fact that, with
the HTTP protocol for instance, the library needs to know in advance the
total number of bytes that will be sent.
Examples:
The module http.lua provides functionality to download an URL
from an HTTP server. The implementation conforms to the HTTP/1.1
standard,
RFC 2068.
URLs MUST conform to
RFC 1738,
that is, an URL is a string in the form:
The module exports three functions:
http_request(method, url [, req_hdrs, req_body, stay])
Performs the generic HTTP request using method on url
sending the request headers req_hdrs and request body
req_body in the request message. If successful, the function
returns the body of the reply, a table with the response headers and the
HTTP status returned by the server. In case of error, the function
returns whatever it managed to retrieve (nil values
representing failure) and an error message describing the error. If
<user> and <password> are provided in the
URL, the function uses the Basic Authentication Scheme (see note) to retrieve the document. The stay
parameter, when set to anything but nil, prevents the function
from automatically following a 301 or 302 server redirect message.
http_get(url [, req_hdrs, stay])
Calls http_request with method 'GET'.
Examples:
The HTTP/1.1 standard defines two authentication methods: the Basic
Authentication Scheme and the Digest Authentication Scheme, both
explained in detail in
RFC 2068.
The Basic Authentication Scheme sends <user> and
<password> unencrypted to the server and is therefore
considered unsafe. Unfortunately, by the time of this implementation,
the wide majority of servers and browsers support the Basic Scheme only.
Therefore, this is the method used by the toolkit whenever
authentication is required.
Example:
Calls http_request with method 'POST' sending
body along with the request.
http_requestindirect(method, url, resp_body_cb [, req_hdrs, req_body_cb, stay])
Same as http_request, except that the HTTP response message
body is passed to the receive callback resp_body_cb, chunk by
chunk, instead of being returned in a string. Similarly, the HTTP
request message body will be read from send callback
req_body_cb. In case the request has no body, the parameter
req_body_cb should be passed as nil.
http_getindirect(url, resp_body_cb
[, req_hdrs, stay])
Calls http_requestindirect with method 'GET'.
http_postindirect(url, resp_body_cb,
req_body_cb [, req_hdrs, stay])
Calls http_requestindirect with method 'POST' sending
message body obtained from the send callback req_body_cb along
with the request.
The module ftp.lua provides functions to download and upload
files from and to FTP servers. The implementation conforms to
RFC 959.
URLs MUST conform to
RFC 1738,
that is, an URL is a string in the form:
ftp_get(url [, type])
Downloads the URL url using transfer type type and
returns it as a string.
The parameter type can receive values 'a' (ascii, the
default) or 'b' (binary) and determines the transfer type. If
<path> ends with a '/', a directory listing of
<path> is returned. If successful, the function returns
the file contents as a string. In case of error, the function returns
nil and an error message describing the error.
If no <user> is provided, the function tries to log in as
'anonymous'.
Examples:
Stores a file at url with contents given by the string
data and using transfer type type.
The parameter type can receive values 'a' (ascii, the
default) or 'b' (binary) and determines the transfer type. If
successful, the function returns nil. In case of error, the
function returns a string describing the error.
If no <user> is provided, the function tries to log in as
anonymous.
Examples:
Same as ftp_get, but the file contents are passed to the
receive callback receive_cb chunk by chunk, instead of being
returned as a string.
ftp_putindirect(url, send_cb [, type])
Same as ftp_put, but the file contents will be read from the
user provided send callback send_cb, chunk by chunk.
Function Reference
TCP sockets
The function returns one value for each pattern, followed by a single
error code that can be nil in case of success, the string
'closed' in case the connection was closed before the
transmission was completed or the string 'timeout' in case
there was a timeout during the operation. The difference in performance
between all patterns is negligible.
send(socket, string1 [, string2, ... stringN])
-- waits for input on three sockets and broadcasts any received lines
server = bind("localhost", 8080)
a = server:accept(); b = server:accept(); c = server:accept()
a:timeout(1); b:timeout(1); c:timeout(1)
while 1 do
r, _, e = select({a,b,c}, nil)
for i,v in r do
l, e = v:receive()
if e then exit() end
_, s, e = select(nil, {a,b,c}, 1)
for j,u in s do
e = u:send(l, "\n")
if e then exit() end
end
end
end
The nil timeout value allows operations to block
indefinitely. Negative timeout values have the same effect. UDP sockets
The supported options and their expected value types are:
options = {
["option-1-name"] = value-1,
["option-2-name"] = value-2,
["option-3-name"] = value-3,
... ...
["option-n-name"] = value-n
}
Note: UDP socket options should only be used by advanced users.
DNS Services
Note that the alias list can be empty.
resolved = {
["name"] = "canonic-name",
["alias"] = alias-list,
["ip"] = ip-address-list
}
Protocol Reference
SMTP
The module exports two functions:
headers = {
["field-1-name"] = "field-1-value",
["field-2-name"] = "field-2-value",
["field-3-name"] = "field-3-value",
... ...
["field-n-name"] = "field-n-value"
}
mail {
headers = {
to = "fulano@tecgraf.puc-rio.br, beltrano@tecgraf.puc-rio.br",
subject = "LuaSocket test message"
}
from = "luasocket@tecgraf.puc-rio.br"
rcpt = {
"fulano@tecgraf.puc-rio.br",
"beltrano@tecgraf.puc-rio.br",
"sicrano@tecgraf.puc-rio.br"
}
body = "This is a test message. Please ignore."
server = "local"
-- connects to server "local" and sends a message to users
-- "fulano@tecgraf.puc-rio.br" and "beltrano@tecgraf.puc-rio.br"
-- "sicrano@tecgraf.puc-rio.br" receives a 'blind carbon copy' of the message.
e = smtp_mail(from, rcpt, headers, body, server)
to=tolist,
from=from,
subject=subject,
message=message,
cc=cclist,
bcc=bcclist,
mailserver=server
}
to: A comma-separated list of the e-mails of
the recipients of the message. from: The email of the sender. subject: Optional message subject. message: Optional message body. cc: An optional comma-separated list of the
e-mails of "carbon-copy" recipients. bcc: An optional comma-separated list of the
e-mails of the "blind carbon-copy" recipients. mailserver: Address of SMTP server to be used. Streaming with callbacks
receive_cb(chunk, err)
Instead of forcing the Lua application to pass the whole FTP file
contents or the HTTP request message body as a string to the library,
the library allows the user to provide a send callback that will
be called repeatedly to return successive parts of the information
needed by the library:
-- saves incoming file to disk, receiving chunk by chunk
local file = openfile(tmpname(), "wb") -- open temp file for writing
local receive_cb = function(chunk, err)
if chunk and chunk ~= "" then
local res, err = write(%file, chunk) -- try to write to disk
if not res then return nil, err -- woops! abort with error message
else return 1 end -- all is ok, please go on
else closefile(%file) end -- we are done
end
-- print throughput feedback while receiving data
local aux = { start = _time(), got = 0 } -- auxiliar table
local receive_cb = function(chunk, err)
local dt = _time() - %aux.start -- elapsed time since start
if not chunk or chunk == "" then return end
%aux.got = %aux.got + strlen(chunk) -- total bytes received
if dt < 0.1 then return 1 end -- not enough time for estimate
local rate = %aux.got / dt -- get download rate
write("\r" .. format("%.2f", rate)) -- print estimate
return 1 -- ready for more
end
send_cb()
-- sends data from file, in blocks of 4096 bytes
local file = openfile("words", "rb") -- open file for reading
local size = seek(file, "end") -- get file size
seek(file, "set")
local send_cb = function()
local chunk = read(%file, 4096) -- try to read chunk
if not chunk then -- are we done?
closefile(%file)
return "", %size
else return chunk, %size end -- if not, send what we got
end
-- sends everything in the first call
local file = openfile("words", "rb") -- open file for reading
local data = read(file, "*a")
closefile(file)
local send_cb = function()
return %data, strlen(%data) -- return everything
end
HTTP
MIME Headers are represented as a table in the form:[http://][<user>[:<password>]@]<>[:<port>][/<path>]
Field names are case insensitive (as specified by the standard) and all
API functions work with lowercase field names. Field values are left
unmodified.
headers = {
["field-1-name"] = "field-1-value",
["field-2-name"] = "field-2-value",
["field-3-name"] = "field-3-value",
... ...
["field-n-name"] = "field-n-value"
}
Note: Some URLs are protected by their servers from
anonymous download. For those URLs, the server must receive some sort of
authentication along with the request or it will deny download and
return status "401 Authentication Required".
-- connect to server "www.tecgraf.puc-rio.br" and retrieves this manual
-- file from "~diego/luasocket/manual.html"
f, m, s, e = http_get("http://www.tecgraf.puc-rio.br/~diego/luasocket/manual.html")
-- connect to server "www.tecgraf.puc-rio.br" and tries to retrieve
-- "~diego/auth/index.html". Fails because authentication is needed.
f, m, s, e = http_get("http://www.tecgraf.puc-rio.br/~diego/auth/index.html")
-- s returns with value "401 Authentication Required"
http_post(url, req_body [, req_hdrs, stay])
-- connect to server "www.tecgraf.puc-rio.br" and tries to retrieve
-- "~diego/auth/index.html", using the provided name and password to
-- authenticate the request
f, m, s, e = http_get("http://diego:nehab@www.tecgraf.puc-rio.br/~diego/auth/index.html")
-- alternatively, one could fill the appropriate header and authenticate
-- the request directly. both calls are equivalent
h = {authentication = "Basic " .. base64("diego:nehab")}
f, m, s, e = http_get("http://www.tecgraf.puc-rio.br/~diego/auth/index.html", h)
FTP
The module exports two functions: [ftp://][<user>[:<password>]@]<>[:<port>][/<path>]
ftp_put(url, data [, type])
-- log as user "anonymous" on server "ftp.tecgraf.puc-rio.br"
-- go to directory "pub/lua" and get file "lua.tar.gz" as binary.
f, e = ftp_get("ftp://ftp.tecgraf.puc-rio.br/pub/lua/lua.tar.gz", "b")
-- log as user "anonymous" on server "ftp.tecgraf.puc-rio.br"
-- go to director "pub" and retrieve directory listing of directory "lua"
f, e = ftp_get("ftp://ftp.tecgraf.puc-rio.br/pub/lua/")
-- log as user "diego", password "nehab", on server "derain.tecgraf.puc-rio.br"
-- go to directory "tec/luasocket/html" and retrieve file "manual.html"
-- (actually, fails because of wrong password :-)
f, e = ftp_get("ftp://diego:nehab@derain.tecgraf.puc-rio.br/tec/luasocket/html/manual.html")
ftp_getindirect(url, receive_cb [, type])
-- log as user "anonymous" on server "ftp.free.org" and store file
-- "hello" with contents "hello world!" on current directory
e = ftp_put("ftp://ftp.free.org/hello", "hello world!")
| and |
|
|
Last modified by Diego Nehab on Wed Jun 6 19:15:34 EST 2001 | ||