ftp.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html>
  4. <head>
  5. <meta name="description" content="LuaSocket: FTP support">
  6. <meta name="keywords" content="Lua, LuaSocket, FTP, Network, Library, Support">
  7. <title>LuaSocket: FTP support</title>
  8. <link rel="stylesheet" href="reference.css" type="text/css">
  9. </head>
  10. <body>
  11. <!-- header ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  12. <div class=header>
  13. <hr>
  14. <center>
  15. <table summary="LuaSocket logo">
  16. <tr><td align=center><a href="http://www.lua.org">
  17. <img width=128 height=128 border=0 alt="LuaSocket" src="luasocket.png">
  18. </a></td></tr>
  19. <tr><td align=center valign=top>Network support for the Lua language
  20. </td></tr>
  21. </table>
  22. <p class=bar>
  23. <a href="index.html">home</a> &middot;
  24. <a href="index.html#download">download</a> &middot;
  25. <a href="installation.html">installation</a> &middot;
  26. <a href="introduction.html">introduction</a> &middot;
  27. <a href="reference.html">reference</a>
  28. </p>
  29. </center>
  30. <hr>
  31. </div>
  32. <!-- ftp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  33. <h2 id=ftp>FTP</h2>
  34. <p>
  35. FTP (File Transfer Protocol) is a protocol used to transfer files
  36. between hosts. The <tt>ftp</tt> namespace offers thorough support
  37. to FTP, under a simple interface. The implementation conforms to
  38. <a href="http://www.ietf.org/rfc/rfc959.txt">RFC 959</a>.
  39. </p>
  40. <p>
  41. High level functions are provided supporting the most common operations.
  42. These high level functions are implemented on top of a lower level
  43. interface. Using the low-level interface, users can easily create their
  44. own functions to access <em>any</em> operation supported by the FTP
  45. protocol. For that, check the implementation.
  46. </p>
  47. <p>
  48. To really benefit from this module, a good understanding of
  49. <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">
  50. LTN012, Filters sources and sinks</a> is necessary.
  51. </p>
  52. <p>
  53. To obtain the <tt>ftp</tt> namespace, run:
  54. </p>
  55. <pre class=example>
  56. -- loads the FTP module and any libraries it requires
  57. local ftp = require("socket.ftp")
  58. </pre>
  59. <p>
  60. URLs MUST conform to
  61. <a href="http://www.ietf.org/rfc/rfc1738.txt">RFC 1738</a>,
  62. that is, an URL is a string in the form:
  63. </p>
  64. <blockquote>
  65. <tt>
  66. [ftp://][&lt;user&gt;[:&lt;password&gt;]@]&lt;host&gt;[:&lt;port&gt;][/&lt;path&gt;][<i>type</i>=a|i]</tt>
  67. </blockquote>
  68. <p>
  69. The following constants in the namespace can be set to control the default behavior of
  70. the FTP module:
  71. </p>
  72. <ul>
  73. <li> <tt>PASSWORD</tt>: default anonymous password.
  74. <li> <tt>TIMEOUT</tt>: sets the timeout for all I/O operations;
  75. <li> <tt>USER</tt>: default anonymous user;
  76. </ul>
  77. <!-- ftp.get ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  78. <p class=name id=get>
  79. ftp.<b>get(</b>url<b>)</b><br>
  80. ftp.<b>get{</b><br>
  81. &nbsp;&nbsp;host = <i>string</i>,<br>
  82. &nbsp;&nbsp;sink = <i>LTN12 sink</i>,<br>
  83. &nbsp;&nbsp;argument <i>or</i> path = <i>string</i>,<br>
  84. &nbsp;&nbsp;[user = <i>string</i>,]<br>
  85. &nbsp;&nbsp;[password = <i>string</i>]<br>
  86. &nbsp;&nbsp;[command = <i>string</i>,]<br>
  87. &nbsp;&nbsp;[port = <i>number</i>,]<br>
  88. &nbsp;&nbsp;[type = <i>string</i>,]<br>
  89. &nbsp;&nbsp;[step = <i>LTN12 pump step</i>,]<br>
  90. &nbsp;&nbsp;[create = <i>function</i>]<br>
  91. <b>}</b>
  92. </p>
  93. <p class=description>
  94. The <tt>get</tt> function has two forms. The simple form has fixed
  95. functionality: it downloads the contents of a URL and returns it as a
  96. string. The generic form allows a <em>lot</em> more control, as explained
  97. below.
  98. </p>
  99. <p class=parameters>
  100. If the argument of the <tt>get</tt> function is a table, the function
  101. expects at least the fields <tt>host</tt>, <tt>sink</tt>, and one of
  102. <tt>argument</tt> or <tt>path</tt> (<tt>argument</tt> takes
  103. precedence). <tt>Host</tt> is the server to connect to. <tt>Sink</tt> is
  104. the <em>simple</em>
  105. <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a>
  106. sink that will receive the downloaded data. <tt>Argument</tt> or
  107. <tt>path</tt> give the target path to the resource in the server. The
  108. optional arguments are the following:
  109. </p>
  110. <ul>
  111. <li><tt>user</tt>, <tt>password</tt>: User name and password used for
  112. authentication. Defaults to "<tt>ftp:[email protected]</tt>";
  113. <li><tt>command</tt>: The FTP command used to obtain data. Defaults to
  114. "<tt>retr</tt>", but see example below;
  115. <li><tt>port</tt>: The port to used for the control connection. Defaults to 21;
  116. <li><tt>type</tt>: The transfer mode. Can take values "<tt>i</tt>" or
  117. "<tt>a</tt>". Defaults to whatever is the server default;
  118. <li><tt>step</tt>:
  119. <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a>
  120. pump step function used to pass data from the
  121. server to the sink. Defaults to the LTN12 <tt>pump.step</tt> function;
  122. <li><tt>create</tt>: An optional function to be used instead of
  123. <a href=tcp.html#socket.tcp><tt>socket.tcp</tt></a> when the communications socket is created.
  124. </ul>
  125. <p class=return>
  126. If successful, the simple version returns the URL contents as a
  127. string, and the generic function returns 1. In case of error, both
  128. functions return <b><tt>nil</tt></b> and an error message describing the
  129. error.
  130. </p>
  131. <pre class=example>
  132. -- load the ftp support
  133. local ftp = require("socket.ftp")
  134. -- Log as user "anonymous" on server "ftp.tecgraf.puc-rio.br",
  135. -- and get file "lua.tar.gz" from directory "pub/lua" as binary.
  136. f, e = ftp.get("ftp://ftp.tecgraf.puc-rio.br/pub/lua/lua.tar.gz;type=i")
  137. </pre>
  138. <pre class=example>
  139. -- load needed modules
  140. local ftp = require("socket.ftp")
  141. local ltn12 = require("ltn12")
  142. local url = require("socket.url")
  143. -- a function that returns a directory listing
  144. function nlst(u)
  145. local t = {}
  146. local p = url.parse(u)
  147. p.command = "nlst"
  148. p.sink = ltn12.sink.table(t)
  149. local r, e = ftp.get(p)
  150. return r and table.concat(t), e
  151. end
  152. </pre>
  153. <!-- put ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  154. <p class=name id=put>
  155. ftp.<b>put(</b>url, content<b>)</b><br>
  156. ftp.<b>put{</b><br>
  157. &nbsp;&nbsp;host = <i>string</i>,<br>
  158. &nbsp;&nbsp;source = <i>LTN12 sink</i>,<br>
  159. &nbsp;&nbsp;argument <i>or</i> path = <i>string</i>,<br>
  160. &nbsp;&nbsp;[user = <i>string</i>,]<br>
  161. &nbsp;&nbsp;[password = <i>string</i>]<br>
  162. &nbsp;&nbsp;[command = <i>string</i>,]<br>
  163. &nbsp;&nbsp;[port = <i>number</i>,]<br>
  164. &nbsp;&nbsp;[type = <i>string</i>,]<br>
  165. &nbsp;&nbsp;[step = <i>LTN12 pump step</i>,]<br>
  166. &nbsp;&nbsp;[create = <i>function</i>]<br>
  167. <b>}</b>
  168. </p>
  169. <p class=description>
  170. The <tt>put</tt> function has two forms. The simple form has fixed
  171. functionality: it uploads a string of content into a URL. The generic form
  172. allows a <em>lot</em> more control, as explained below.
  173. </p>
  174. <p class=parameters>
  175. If the argument of the <tt>put</tt> function is a table, the function
  176. expects at least the fields <tt>host</tt>, <tt>source</tt>, and one of
  177. <tt>argument</tt> or <tt>path</tt> (<tt>argument</tt> takes
  178. precedence). <tt>Host</tt> is the server to connect to. <tt>Source</tt> is
  179. the <em>simple</em>
  180. <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a>
  181. source that will provide the contents to be uploaded.
  182. <tt>Argument</tt> or
  183. <tt>path</tt> give the target path to the resource in the server. The
  184. optional arguments are the following:
  185. </p>
  186. <ul>
  187. <li><tt>user</tt>, <tt>password</tt>: User name and password used for
  188. authentication. Defaults to "<tt>ftp:[email protected]</tt>";
  189. <li><tt>command</tt>: The FTP command used to send data. Defaults to
  190. "<tt>stor</tt>", but see example below;
  191. <li><tt>port</tt>: The port to used for the control connection. Defaults to 21;
  192. <li><tt>type</tt>: The transfer mode. Can take values "<tt>i</tt>" or
  193. "<tt>a</tt>". Defaults to whatever is the server default;
  194. <li><tt>step</tt>:
  195. <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a>
  196. pump step function used to pass data from the
  197. server to the sink. Defaults to the LTN12 <tt>pump.step</tt> function;
  198. <li><tt>create</tt>: An optional function to be used instead of
  199. <a href=tcp.html#socket.tcp><tt>socket.tcp</tt></a> when the communications socket is created.
  200. </ul>
  201. <p class=return>
  202. Both functions return 1 if successful, or <b><tt>nil</tt></b> and an error
  203. message describing the reason for failure.
  204. </p>
  205. <pre class=example>
  206. -- load the ftp support
  207. local ftp = require("socket.ftp")
  208. -- Log as user "fulano" on server "ftp.example.com",
  209. -- using password "silva", and store a file "README" with contents
  210. -- "wrong password, of course"
  211. f, e = ftp.put("ftp://fulano:[email protected]/README",
  212. "wrong password, of course")
  213. </pre>
  214. <pre class=example>
  215. -- load the ftp support
  216. local ftp = require("socket.ftp")
  217. local ltn12 = require("ltn12")
  218. -- Log as user "fulano" on server "ftp.example.com",
  219. -- using password "silva", and append to the remote file "LOG", sending the
  220. -- contents of the local file "LOCAL-LOG"
  221. f, e = ftp.put{
  222. host = "ftp.example.com",
  223. user = "fulano",
  224. password = "silva",
  225. command = "appe",
  226. argument = "LOG",
  227. source = ltn12.source.file(io.open("LOCAL-LOG", "r"))
  228. }
  229. </pre>
  230. <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  231. <div class=footer>
  232. <hr>
  233. <center>
  234. <p class=bar>
  235. <a href="index.html">home</a> &middot;
  236. <a href="index.html#download">download</a> &middot;
  237. <a href="installation.html">installation</a> &middot;
  238. <a href="introduction.html">introduction</a> &middot;
  239. <a href="reference.html">reference</a>
  240. </p>
  241. <p>
  242. <small>
  243. Last modified by Diego Nehab on <br>
  244. Thu Apr 20 00:25:18 EDT 2006
  245. </small>
  246. </p>
  247. </center>
  248. </div>
  249. </body>
  250. </html>