|
Packit |
b53373 |
|
|
Packit |
b53373 |
"http://www.w3.org/TR/html4/strict.dtd">
|
|
Packit |
b53373 |
<html>
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
<head>
|
|
Packit |
b53373 |
<meta name="description" content="LuaSocket: FTP support">
|
|
Packit |
b53373 |
<meta name="keywords" content="Lua, LuaSocket, FTP, Network, Library, Support">
|
|
Packit |
b53373 |
<title>LuaSocket: FTP support</title>
|
|
Packit |
b53373 |
<link rel="stylesheet" href="reference.css" type="text/css">
|
|
Packit |
b53373 |
</head>
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
<body>
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
<center>
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
Network support for the Lua language
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
home ·
|
|
Packit |
b53373 |
download ·
|
|
Packit |
b53373 |
installation ·
|
|
Packit |
b53373 |
introduction ·
|
|
Packit |
b53373 |
reference
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
</center>
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
FTP
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
FTP (File Transfer Protocol) is a protocol used to transfer files
|
|
Packit |
b53373 |
between hosts. The <tt>ftp</tt> namespace offers thorough support
|
|
Packit |
b53373 |
to FTP, under a simple interface. The implementation conforms to
|
|
Packit |
b53373 |
RFC 959.
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
High level functions are provided supporting the most common operations.
|
|
Packit |
b53373 |
These high level functions are implemented on top of a lower level
|
|
Packit |
b53373 |
interface. Using the low-level interface, users can easily create their
|
|
Packit |
b53373 |
own functions to access any operation supported by the FTP
|
|
Packit |
b53373 |
protocol. For that, check the implementation.
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
To really benefit from this module, a good understanding of
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
LTN012, Filters sources and sinks is necessary.
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
To obtain the <tt>ftp</tt> namespace, run:
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
-- loads the FTP module and any libraries it requires
|
|
Packit |
b53373 |
local ftp = require("socket.ftp")
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
URLs MUST conform to
|
|
Packit |
b53373 |
RFC 1738,
|
|
Packit |
b53373 |
that is, an URL is a string in the form:
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
<tt>
|
|
Packit |
b53373 |
[ftp://][<user>[:<password>]@]<host>[:<port>][/<path>][type=a|i]</tt>
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
The following constants in the namespace can be set to control the default behavior of
|
|
Packit |
b53373 |
the FTP module:
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
<tt>PASSWORD</tt>: default anonymous password.
|
|
Packit |
b53373 |
<tt>PORT</tt>: default port used for the control connection;
|
|
Packit |
b53373 |
<tt>TIMEOUT</tt>: sets the timeout for all I/O operations;
|
|
Packit |
b53373 |
<tt>USER</tt>: default anonymous user;
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
ftp.get(url)
|
|
Packit |
b53373 |
ftp.get{
|
|
Packit |
b53373 |
host = string,
|
|
Packit |
b53373 |
sink = LTN12 sink,
|
|
Packit |
b53373 |
argument or path = string,
|
|
Packit |
b53373 |
[user = string,]
|
|
Packit |
b53373 |
[password = string]
|
|
Packit |
b53373 |
[command = string,]
|
|
Packit |
b53373 |
[port = number,]
|
|
Packit |
b53373 |
[type = string,]
|
|
Packit |
b53373 |
[step = LTN12 pump step,]
|
|
Packit |
b53373 |
[create = function]
|
|
Packit |
b53373 |
}
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
The <tt>get</tt> function has two forms. The simple form has fixed
|
|
Packit |
b53373 |
functionality: it downloads the contents of a URL and returns it as a
|
|
Packit |
b53373 |
string. The generic form allows a lot more control, as explained
|
|
Packit |
b53373 |
below.
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
If the argument of the <tt>get</tt> function is a table, the function
|
|
Packit |
b53373 |
expects at least the fields <tt>host</tt>, <tt>sink</tt>, and one of
|
|
Packit |
b53373 |
<tt>argument</tt> or <tt>path</tt> (<tt>argument</tt> takes
|
|
Packit |
b53373 |
precedence). <tt>Host</tt> is the server to connect to. <tt>Sink</tt> is
|
|
Packit |
b53373 |
the simple
|
|
Packit |
b53373 |
LTN12
|
|
Packit |
b53373 |
sink that will receive the downloaded data. <tt>Argument</tt> or
|
|
Packit |
b53373 |
<tt>path</tt> give the target path to the resource in the server. The
|
|
Packit |
b53373 |
optional arguments are the following:
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
<tt>user</tt>, <tt>password</tt>: User name and password used for
|
|
Packit |
b53373 |
authentication. Defaults to "<tt>ftp:anonymous@anonymous.org</tt>";
|
|
Packit |
b53373 |
<tt>command</tt>: The FTP command used to obtain data. Defaults to
|
|
Packit |
b53373 |
"<tt>retr</tt>", but see example below;
|
|
Packit |
b53373 |
<tt>port</tt>: The port to used for the control connection. Defaults to 21;
|
|
Packit |
b53373 |
<tt>type</tt>: The transfer mode. Can take values "<tt>i</tt>" or
|
|
Packit |
b53373 |
"<tt>a</tt>". Defaults to whatever is the server default;
|
|
Packit |
b53373 |
<tt>step</tt>:
|
|
Packit |
b53373 |
LTN12
|
|
Packit |
b53373 |
pump step function used to pass data from the
|
|
Packit |
b53373 |
server to the sink. Defaults to the LTN12 <tt>pump.step</tt> function;
|
|
Packit |
b53373 |
<tt>create</tt>: An optional function to be used instead of
|
|
Packit |
b53373 |
<tt>socket.tcp</tt> when the communications socket is created.
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
If successful, the simple version returns the URL contents as a
|
|
Packit |
b53373 |
string, and the generic function returns 1. In case of error, both
|
|
Packit |
b53373 |
functions return <tt>nil</tt> and an error message describing the
|
|
Packit |
b53373 |
error.
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
-- load the ftp support
|
|
Packit |
b53373 |
local ftp = require("socket.ftp")
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
-- Log as user "anonymous" on server "ftp.tecgraf.puc-rio.br",
|
|
Packit |
b53373 |
-- and get file "lua.tar.gz" from directory "pub/lua" as binary.
|
|
Packit |
b53373 |
f, e = ftp.get("ftp://ftp.tecgraf.puc-rio.br/pub/lua/lua.tar.gz;type=i")
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
-- load needed modules
|
|
Packit |
b53373 |
local ftp = require("socket.ftp")
|
|
Packit |
b53373 |
local ltn12 = require("ltn12")
|
|
Packit |
b53373 |
local url = require("socket.url")
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
-- a function that returns a directory listing
|
|
Packit |
b53373 |
function nlst(u)
|
|
Packit |
b53373 |
local t = {}
|
|
Packit |
b53373 |
local p = url.parse(u)
|
|
Packit |
b53373 |
p.command = "nlst"
|
|
Packit |
b53373 |
p.sink = ltn12.sink.table(t)
|
|
Packit |
b53373 |
local r, e = ftp.get(p)
|
|
Packit |
b53373 |
return r and table.concat(t), e
|
|
Packit |
b53373 |
end
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
ftp.put(url, content)
|
|
Packit |
b53373 |
ftp.put{
|
|
Packit |
b53373 |
host = string,
|
|
Packit |
b53373 |
source = LTN12 sink,
|
|
Packit |
b53373 |
argument or path = string,
|
|
Packit |
b53373 |
[user = string,]
|
|
Packit |
b53373 |
[password = string]
|
|
Packit |
b53373 |
[command = string,]
|
|
Packit |
b53373 |
[port = number,]
|
|
Packit |
b53373 |
[type = string,]
|
|
Packit |
b53373 |
[step = LTN12 pump step,]
|
|
Packit |
b53373 |
[create = function]
|
|
Packit |
b53373 |
}
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
The <tt>put</tt> function has two forms. The simple form has fixed
|
|
Packit |
b53373 |
functionality: it uploads a string of content into a URL. The generic form
|
|
Packit |
b53373 |
allows a lot more control, as explained below.
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
If the argument of the <tt>put</tt> function is a table, the function
|
|
Packit |
b53373 |
expects at least the fields <tt>host</tt>, <tt>source</tt>, and one of
|
|
Packit |
b53373 |
<tt>argument</tt> or <tt>path</tt> (<tt>argument</tt> takes
|
|
Packit |
b53373 |
precedence). <tt>Host</tt> is the server to connect to. <tt>Source</tt> is
|
|
Packit |
b53373 |
the simple
|
|
Packit |
b53373 |
LTN12
|
|
Packit |
b53373 |
source that will provide the contents to be uploaded.
|
|
Packit |
b53373 |
<tt>Argument</tt> or
|
|
Packit |
b53373 |
<tt>path</tt> give the target path to the resource in the server. The
|
|
Packit |
b53373 |
optional arguments are the following:
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
<tt>user</tt>, <tt>password</tt>: User name and password used for
|
|
Packit |
b53373 |
authentication. Defaults to "<tt>ftp:anonymous@anonymous.org</tt>";
|
|
Packit |
b53373 |
<tt>command</tt>: The FTP command used to send data. Defaults to
|
|
Packit |
b53373 |
"<tt>stor</tt>", but see example below;
|
|
Packit |
b53373 |
<tt>port</tt>: The port to used for the control connection. Defaults to 21;
|
|
Packit |
b53373 |
<tt>type</tt>: The transfer mode. Can take values "<tt>i</tt>" or
|
|
Packit |
b53373 |
"<tt>a</tt>". Defaults to whatever is the server default;
|
|
Packit |
b53373 |
<tt>step</tt>:
|
|
Packit |
b53373 |
LTN12
|
|
Packit |
b53373 |
pump step function used to pass data from the
|
|
Packit |
b53373 |
server to the sink. Defaults to the LTN12 <tt>pump.step</tt> function;
|
|
Packit |
b53373 |
<tt>create</tt>: An optional function to be used instead of
|
|
Packit |
b53373 |
<tt>socket.tcp</tt> when the communications socket is created.
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
Both functions return 1 if successful, or <tt>nil</tt> and an error
|
|
Packit |
b53373 |
message describing the reason for failure.
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
-- load the ftp support
|
|
Packit |
b53373 |
local ftp = require("socket.ftp")
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
-- Log as user "fulano" on server "ftp.example.com",
|
|
Packit |
b53373 |
-- using password "silva", and store a file "README" with contents
|
|
Packit |
b53373 |
-- "wrong password, of course"
|
|
Packit |
b53373 |
f, e = ftp.put("ftp://fulano:silva@ftp.example.com/README",
|
|
Packit |
b53373 |
"wrong password, of course")
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
-- load the ftp support
|
|
Packit |
b53373 |
local ftp = require("socket.ftp")
|
|
Packit |
b53373 |
local ltn12 = require("ltn12")
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
-- Log as user "fulano" on server "ftp.example.com",
|
|
Packit |
b53373 |
-- using password "silva", and append to the remote file "LOG", sending the
|
|
Packit |
b53373 |
-- contents of the local file "LOCAL-LOG"
|
|
Packit |
b53373 |
f, e = ftp.put{
|
|
Packit |
b53373 |
host = "ftp.example.com",
|
|
Packit |
b53373 |
user = "fulano",
|
|
Packit |
b53373 |
password = "silva",
|
|
Packit |
b53373 |
command = "appe",
|
|
Packit |
b53373 |
argument = "LOG",
|
|
Packit |
b53373 |
source = ltn12.source.file(io.open("LOCAL-LOG", "r"))
|
|
Packit |
b53373 |
}
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
<center>
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
home ·
|
|
Packit |
b53373 |
download ·
|
|
Packit |
b53373 |
installation ·
|
|
Packit |
b53373 |
introduction ·
|
|
Packit |
b53373 |
reference
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
<small>
|
|
Packit |
b53373 |
Last modified by Diego Nehab on
|
|
Packit |
b53373 |
Thu Apr 20 00:25:18 EDT 2006
|
|
Packit |
b53373 |
</small>
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
</center>
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
|
|
Packit |
b53373 |
</body>
|
|
Packit |
b53373 |
</html>
|