1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
6 <meta name=
"description" content=
"LuaSocket: FTP support">
7 <meta name=
"keywords" content=
"Lua, LuaSocket, FTP, Network, Library, Support">
8 <title>LuaSocket: FTP support
</title>
9 <link rel=
"stylesheet" href=
"reference.css" type=
"text/css">
14 <!-- header ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
19 <table summary=
"LuaSocket logo">
20 <tr><td align=center
><a href=
"http://www.lua.org">
21 <img width=
128 height=
128 border=
0 alt=
"LuaSocket" src=
"luasocket.png">
23 <tr><td align=center valign=top
>Network support for the Lua language
27 <a href=
"index.html">home
</a> ·
28 <a href=
"index.html#download">download
</a> ·
29 <a href=
"installation.html">installation
</a> ·
30 <a href=
"introduction.html">introduction
</a> ·
31 <a href=
"reference.html">reference
</a>
37 <!-- ftp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
42 FTP (File Transfer Protocol) is a protocol used to transfer files
43 between hosts. The
<tt>ftp
</tt> namespace offers thorough support
44 to FTP, under a simple interface. The implementation conforms to
45 <a href=
"http://www.ietf.org/rfc/rfc959.txt">RFC
959</a>.
49 High level functions are provided supporting the most common operations.
50 These high level functions are implemented on top of a lower level
51 interface. Using the low-level interface, users can easily create their
52 own functions to access
<em>any
</em> operation supported by the FTP
53 protocol. For that, check the implementation.
57 To really benefit from this module, a good understanding of
58 <a href=
"http://lua-users.org/wiki/FiltersSourcesAndSinks">
59 LTN012, Filters sources and sinks
</a> is necessary.
63 To obtain the
<tt>ftp
</tt> namespace, run:
67 -- loads the FTP module and any libraries it requires
68 local ftp = require(
"socket.ftp")
73 <a href=
"http://www.ietf.org/rfc/rfc1738.txt">RFC
1738</a>,
74 that is, an URL is a string in the form:
79 [ftp://][
<user
>[:
<password
>]@]
<host
>[:
<port
>][/
<path
>][
<i>type
</i>=a|i]
</tt>
83 The following constants in the namespace can be set to control the default behavior of
88 <li> <tt>PASSWORD
</tt>: default anonymous password.
89 <li> <tt>PORT
</tt>: default port used for the control connection;
90 <li> <tt>TIMEOUT
</tt>: sets the timeout for all I/O operations;
91 <li> <tt>USER
</tt>: default anonymous user;
95 <!-- ftp.get ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
98 ftp.
<b>get(
</b>url
<b>)
</b><br>
100 host =
<i>string
</i>,
<br>
101 sink =
<i>LTN12 sink
</i>,
<br>
102 argument
<i>or
</i> path =
<i>string
</i>,
<br>
103 [user =
<i>string
</i>,]
<br>
104 [password =
<i>string
</i>]
<br>
105 [command =
<i>string
</i>,]
<br>
106 [port =
<i>number
</i>,]
<br>
107 [type =
<i>string
</i>,]
<br>
108 [step =
<i>LTN12 pump step
</i>,]
<br>
109 [create =
<i>function
</i>]
<br>
113 <p class=description
>
114 The
<tt>get
</tt> function has two forms. The simple form has fixed
115 functionality: it downloads the contents of a URL and returns it as a
116 string. The generic form allows a
<em>lot
</em> more control, as explained
121 If the argument of the
<tt>get
</tt> function is a table, the function
122 expects at least the fields
<tt>host
</tt>,
<tt>sink
</tt>, and one of
123 <tt>argument
</tt> or
<tt>path
</tt> (
<tt>argument
</tt> takes
124 precedence).
<tt>Host
</tt> is the server to connect to.
<tt>Sink
</tt> is
126 <a href=
"http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12
</a>
127 sink that will receive the downloaded data.
<tt>Argument
</tt> or
128 <tt>path
</tt> give the target path to the resource in the server. The
129 optional arguments are the following:
132 <li><tt>user
</tt>,
<tt>password
</tt>: User name and password used for
133 authentication. Defaults to
"<tt>ftp:anonymous@anonymous.org</tt>";
134 <li><tt>command
</tt>: The FTP command used to obtain data. Defaults to
135 "<tt>retr</tt>", but see example below;
136 <li><tt>port
</tt>: The port to used for the control connection. Defaults to
21;
137 <li><tt>type
</tt>: The transfer mode. Can take values
"<tt>i</tt>" or
138 "<tt>a</tt>". Defaults to whatever is the server default;
140 <a href=
"http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12
</a>
141 pump step function used to pass data from the
142 server to the sink. Defaults to the LTN12
<tt>pump.step
</tt> function;
143 <li><tt>create
</tt>: An optional function to be used instead of
144 <a href=tcp.html#socket.tcp
><tt>socket.tcp
</tt></a> when the communications socket is created.
148 If successful, the simple version returns the URL contents as a
149 string, and the generic function returns
1. In case of error, both
150 functions return
<b><tt>nil
</tt></b> and an error message describing the
155 -- load the ftp support
156 local ftp = require(
"socket.ftp")
158 -- Log as user
"anonymous" on server
"ftp.tecgraf.puc-rio.br",
159 -- and get file
"lua.tar.gz" from directory
"pub/lua" as binary.
160 f, e = ftp.get(
"ftp://ftp.tecgraf.puc-rio.br/pub/lua/lua.tar.gz;type=i")
164 -- load needed modules
165 local ftp = require(
"socket.ftp")
166 local ltn12 = require(
"ltn12")
167 local url = require(
"socket.url")
169 -- a function that returns a directory listing
172 local p = url.parse(u)
174 p.sink = ltn12.sink.table(t)
175 local r, e = ftp.get(p)
176 return r and table.concat(t), e
180 <!-- put ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
182 <p class=name id=put
>
183 ftp.
<b>put(
</b>url, content
<b>)
</b><br>
185 host =
<i>string
</i>,
<br>
186 source =
<i>LTN12 sink
</i>,
<br>
187 argument
<i>or
</i> path =
<i>string
</i>,
<br>
188 [user =
<i>string
</i>,]
<br>
189 [password =
<i>string
</i>]
<br>
190 [command =
<i>string
</i>,]
<br>
191 [port =
<i>number
</i>,]
<br>
192 [type =
<i>string
</i>,]
<br>
193 [step =
<i>LTN12 pump step
</i>,]
<br>
194 [create =
<i>function
</i>]
<br>
198 <p class=description
>
199 The
<tt>put
</tt> function has two forms. The simple form has fixed
200 functionality: it uploads a string of content into a URL. The generic form
201 allows a
<em>lot
</em> more control, as explained below.
205 If the argument of the
<tt>put
</tt> function is a table, the function
206 expects at least the fields
<tt>host
</tt>,
<tt>source
</tt>, and one of
207 <tt>argument
</tt> or
<tt>path
</tt> (
<tt>argument
</tt> takes
208 precedence).
<tt>Host
</tt> is the server to connect to.
<tt>Source
</tt> is
210 <a href=
"http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12
</a>
211 source that will provide the contents to be uploaded.
213 <tt>path
</tt> give the target path to the resource in the server. The
214 optional arguments are the following:
217 <li><tt>user
</tt>,
<tt>password
</tt>: User name and password used for
218 authentication. Defaults to
"<tt>ftp:anonymous@anonymous.org</tt>";
219 <li><tt>command
</tt>: The FTP command used to send data. Defaults to
220 "<tt>stor</tt>", but see example below;
221 <li><tt>port
</tt>: The port to used for the control connection. Defaults to
21;
222 <li><tt>type
</tt>: The transfer mode. Can take values
"<tt>i</tt>" or
223 "<tt>a</tt>". Defaults to whatever is the server default;
225 <a href=
"http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12
</a>
226 pump step function used to pass data from the
227 server to the sink. Defaults to the LTN12
<tt>pump.step
</tt> function;
228 <li><tt>create
</tt>: An optional function to be used instead of
229 <a href=tcp.html#socket.tcp
><tt>socket.tcp
</tt></a> when the communications socket is created.
233 Both functions return
1 if successful, or
<b><tt>nil
</tt></b> and an error
234 message describing the reason for failure.
238 -- load the ftp support
239 local ftp = require(
"socket.ftp")
241 -- Log as user
"fulano" on server
"ftp.example.com",
242 -- using password
"silva", and store a file
"README" with contents
243 --
"wrong password, of course"
244 f, e = ftp.put(
"ftp://fulano:silva@ftp.example.com/README",
245 "wrong password, of course")
249 -- load the ftp support
250 local ftp = require(
"socket.ftp")
251 local ltn12 = require(
"ltn12")
253 -- Log as user
"fulano" on server
"ftp.example.com",
254 -- using password
"silva", and append to the remote file
"LOG", sending the
255 -- contents of the local file
"LOCAL-LOG"
257 host =
"ftp.example.com",
262 source = ltn12.source.file(io.open(
"LOCAL-LOG",
"r"))
267 <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
273 <a href=
"index.html">home
</a> ·
274 <a href=
"index.html#download">download
</a> ·
275 <a href=
"installation.html">installation
</a> ·
276 <a href=
"introduction.html">introduction
</a> ·
277 <a href=
"reference.html">reference
</a>
281 Last modified by Diego Nehab on
<br>
282 Thu Apr
20 00:
25:
18 EDT
2006