Skip to content

Commit da06aa5

Browse files
authored
Create FTP in python
1 parent 9e514c9 commit da06aa5

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed

FTP in python

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
How to use FTP in Python
2+
Overview
3+
This article will show how you can use FTP in Python with the help of the
4+
ftplib module.
5+
Ftplib
6+
The ftplib module in Python allows you to write Python programs that perform a
7+
variety of automated FTP jobs. You can easily connect to a FTP server to retrieve
8+
files and process them locally.
9+
10+
To use the ftplib module in Python, you first have to import it into your script.
11+
Open a Connection
12+
To "open" a connection to the FTP Server, you have to create the object.
13+
14+
Once the connection is made (opened), you can use the methods in the ftplib
15+
module.
16+
17+
Several methods are available in two flavors: one for handling text files and
18+
another for binary files.
19+
20+
You can easily navigate the directory structure, manage and download files.
21+
How do I use it?
22+
This program will first connect to a FTP server (ftp.cwi.nl) and then list the
23+
files and directories in the FTP server root directory using the LIST() method.
24+
from ftplib import FTP
25+
26+
ftp = FTP('ftp.cwi.nl') # connect to host, default port
27+
28+
ftp.login() # user anonymous, passwd anonymous@
29+
30+
ftp.retrlines('LIST') # list directory contents
31+
Our second program opens a connection to 'ftp.sunet.se' as the user 'anonymous'
32+
with an email address of '[email protected]'
33+
34+
It then lists the files and directories on the FTP server by using the dir()
35+
method.
36+
37+
The output is saved to the 'files' variable.
38+
39+
I then use print to see the files on screen.
40+
41+
If I want I to change directory I would just use ftp.cwd(path) to do so.
42+
43+
To close the FTP connection, use the quit() method.
44+
import ftplib
45+
46+
ftp = ftplib.FTP('ftp.sunet.se', 'anonymous', '[email protected]')
47+
48+
print "File List: "
49+
50+
files = ftp.dir()
51+
52+
print files
53+
54+
ftp.cwd("/pub/unix") #changing to /pub/unix
55+
Common FTP Methods
56+
FTP.connect(host[, port[, timeout]])
57+
58+
Connect to the given host and port.
59+
60+
The default port number is 21, as specified by the FTP protocol specification.
61+
62+
It is rarely needed to specify a different port number.
63+
64+
This function should be called only once for each instance
65+
66+
It should not be called at all if a host was given when the instance was created.
67+
68+
All other methods can only be used after a connection
69+
has been made.
70+
71+
The optional timeout parameter specifies a timeout in seconds for the connection
72+
attempt.
73+
74+
If no timeout is passed, the global default timeout setting will be used.
75+
FTP.getwelcome()
76+
77+
Return the welcome message sent by the server in reply to the initial connection.
78+
79+
This message sometimes contains disclaimers or help information that may be
80+
relevant to the user
81+
FTP.login([user[, passwd[, acct]]])
82+
83+
Log in as the given user.
84+
85+
The passwd and acct parameters are optional and default to the empty string.
86+
87+
If no user is specified, it defaults to 'anonymous'.
88+
89+
If user is 'anonymous', the default passwd is 'anonymous@'.
90+
91+
This function should be called only once for each instance, after a connection
92+
has been established.
93+
94+
It should not be called at all if a host and user were given when the instance
95+
was created.
96+
97+
Most FTP commands are only allowed after the client has logged in.
98+
99+
The acct parameter supplies “accounting information”; few systems implement this.
100+
FTP.retrbinary(command, callback[, maxblocksize[, rest]])
101+
102+
103+
104+
Retrieve a file in binary transfer mode.
105+
106+
Command should be an appropriate RETR command: 'RETR filename'.
107+
108+
The callback function is called for each block of data received, with a single
109+
string argument giving the data block.
110+
111+
The optional maxblocksize argument specifies the maximum chunk size to read on
112+
the low-level socket object created to do the actual transfer.
113+
114+
A reasonable default is chosen. rest means the same thing as in the transfercmd()
115+
method.
116+
FTP.retrlines(command[, callback])
117+
118+
Retrieve a file or directory listing in ASCII transfer mode.
119+
120+
Command should be an appropriate RETR command or a command such as LIST, NLST or
121+
MLSD.
122+
123+
LIST retrieves a list of files and information about those files.
124+
125+
NLST retrieves a list of file names.
126+
127+
On some servers, MLSD retrieves a machine readable list of files and information
128+
about those files.
129+
130+
The callback function is called for each line with a string argument containing
131+
the line with the trailing CRLF stripped.
132+
133+
The default callback prints the line to sys.stdout.
134+
FTP.dir(argument[, ...])
135+
136+
Produce a directory listing as returned by the LIST command, printing it to
137+
standard output.
138+
139+
The optional argument is a directory to list (default is the current server
140+
directory).
141+
142+
Multiple arguments can be used to pass non-standard options to the LIST command.
143+
144+
If the last argument is a function, it is used as a callback function as for
145+
retrlines(); the default prints to sys.stdout.
146+
147+
This method returns None.
148+
FTP.delete(filename)
149+
150+
Remove the file named filename from the server.
151+
152+
If successful, returns the text of the response, otherwise raises error_perm on
153+
permission errors or error_reply on other errors.
154+
FTP.cwd(pathname)
155+
156+
Set the current directory on the server.
157+
FTP.mkd(pathname)
158+
159+
Create a new directory on the server.
160+
FTP.pwd()
161+
162+
Return the pathname of the current directory on the server.
163+
FTP.quit()
164+
165+
Send a QUIT command to the server and close the connection.
166+
167+
This is the “polite” way to close a connection, but it may raise an exception if
168+
the server responds with an error to the QUIT command.
169+
170+
This implies a call to the close() method which renders the FTP instance useless
171+
for subsequent calls.
172+
FTP.close()
173+
174+
Close the connection unilaterally.
175+
176+
This should not be applied to an already closed connection such as after a
177+
successful call to quit().
178+
179+
After this call the FTP instance should not be used any more.
180+
181+
After a call to close() or quit() you cannot reopen the connection by issuing
182+
another login() method).

0 commit comments

Comments
 (0)