12
12
>>> from ftplib import FTP
13
13
>>> ftp = FTP('ftp.python.org') # connect to host, default port
14
14
>>> ftp.login() # default, i.e.: user anonymous, passwd user@hostname
15
+ '230 Guest login ok, access restrictions apply.'
15
16
>>> ftp.retrlines('LIST') # list directory contents
16
17
total 9
17
18
drwxr-xr-x 8 root wheel 1024 Jan 3 1994 .
23
24
drwxr-xr-x 6 1094 wheel 1024 Sep 13 19:07 pub
24
25
drwxr-xr-x 3 root wheel 1024 Jan 3 1994 usr
25
26
-rw-r--r-- 1 root root 312 Aug 1 1994 welcome.msg
27
+ '226 Transfer complete.'
26
28
>>> ftp.quit()
29
+ '221 Goodbye.'
27
30
>>>
28
31
29
32
A nice test that reveals some of the network dialogue would be:
@@ -98,9 +101,11 @@ def __init__(self, host = '', user = '', passwd = '', acct = ''):
98
101
self .sock = None
99
102
self .file = None
100
103
self .welcome = None
104
+ resp = None
101
105
if host :
102
- self .connect (host )
103
- if user : self .login (user , passwd , acct )
106
+ resp = self .connect (host )
107
+ if user : resp = self .login (user , passwd , acct )
108
+ return resp
104
109
105
110
def connect (self , host = '' , port = 0 ):
106
111
'''Connect to host. Arguments are:
@@ -113,6 +118,7 @@ def connect(self, host = '', port = 0):
113
118
self .sock .connect (self .host , self .port )
114
119
self .file = self .sock .makefile ('rb' )
115
120
self .welcome = self .getresp ()
121
+ return self .welcome
116
122
117
123
def getwelcome (self ):
118
124
'''Get the welcome message from the server.
@@ -203,6 +209,7 @@ def voidresp(self):
203
209
resp = self .getresp ()
204
210
if resp [0 ] <> '2' :
205
211
raise error_reply , resp
212
+ return resp
206
213
207
214
def abort (self ):
208
215
'''Abort a file transfer. Uses out-of-band data.
@@ -224,15 +231,15 @@ def sendcmd(self, cmd):
224
231
def voidcmd (self , cmd ):
225
232
"""Send a command and expect a response beginning with '2'."""
226
233
self .putcmd (cmd )
227
- self .voidresp ()
234
+ return self .voidresp ()
228
235
229
236
def sendport (self , host , port ):
230
237
'''Send a PORT command with the current host and the given port number.'''
231
238
hbytes = string .splitfields (host , '.' )
232
239
pbytes = [`port/256` , `port%256` ]
233
240
bytes = hbytes + pbytes
234
241
cmd = 'PORT ' + string .joinfields (bytes , ',' )
235
- self .voidcmd (cmd )
242
+ return self .voidcmd (cmd )
236
243
237
244
def makeport (self ):
238
245
'''Create a new socket and send a PORT command for it.'''
@@ -309,6 +316,7 @@ def login(self, user = '', passwd = '', acct = ''):
309
316
if resp [0 ] == '3' : resp = self .sendcmd ('ACCT ' + acct )
310
317
if resp [0 ] <> '2' :
311
318
raise error_reply , resp
319
+ return resp
312
320
313
321
def retrbinary (self , cmd , callback , blocksize ):
314
322
'''Retrieve data in binary mode.
@@ -323,7 +331,7 @@ def retrbinary(self, cmd, callback, blocksize):
323
331
break
324
332
callback (data )
325
333
conn .close ()
326
- self .voidresp ()
334
+ return self .voidresp ()
327
335
328
336
def retrlines (self , cmd , callback = None ):
329
337
'''Retrieve data in line mode.
@@ -347,7 +355,7 @@ def retrlines(self, cmd, callback = None):
347
355
callback (line )
348
356
fp .close ()
349
357
conn .close ()
350
- self .voidresp ()
358
+ return self .voidresp ()
351
359
352
360
def storbinary (self , cmd , fp , blocksize ):
353
361
'''Store a file in binary mode.'''
@@ -358,7 +366,7 @@ def storbinary(self, cmd, fp, blocksize):
358
366
if not buf : break
359
367
conn .send (buf )
360
368
conn .close ()
361
- self .voidresp ()
369
+ return self .voidresp ()
362
370
363
371
def storlines (self , cmd , fp ):
364
372
'''Store a file in line mode.'''
@@ -372,12 +380,12 @@ def storlines(self, cmd, fp):
372
380
buf = buf + CRLF
373
381
conn .send (buf )
6D40
374
382
conn .close ()
375
- self .voidresp ()
383
+ return self .voidresp ()
376
384
377
385
def acct (self , password ):
378
386
'''Send new account name.'''
379
387
cmd = 'ACCT ' + password
380
- self .voidcmd (cmd )
388
+ return self .voidcmd (cmd )
381
389
382
390
def nlst (self , * args ):
383
391
'''Return a list of files in a given directory (default the current).'''
@@ -408,13 +416,13 @@ def rename(self, fromname, toname):
408
416
resp = self .sendcmd ('RNFR ' + fromname )
409
417
if resp [0 ] <> '3' :
410
418
raise error_reply , resp
411
- self .voidcmd ('RNTO ' + toname )
419
+ return self .voidcmd ('RNTO ' + toname )
412
420
413
421
def delete (self , filename ):
414
422
'''Delete a file.'''
415
423
resp = self .sendcmd ('DELE ' + filename )
416
424
if resp [:3 ] == '250' :
417
- return
425
+ return resp
418
426
elif resp [:1 ] == '5' :
419
427
raise error_perm , resp
420
428
else :
@@ -424,13 +432,12 @@ def cwd(self, dirname):
424
432
'''Change to a directory.'''
425
433
if dirname == '..' :
426
434
try :
427
- self .voidcmd ('CDUP' )
428
- return
435
+ return self .voidcmd ('CDUP' )
429
436
except error_perm , msg :
430
437
if msg [:3 ] != '500' :
431
438
raise error_perm , msg
432
439
cmd = 'CWD ' + dirname
433
- self .voidcmd (cmd )
440
+ return self .voidcmd (cmd )
434
441
435
442
def size (self , filename ):
436
443
'''Retrieve the size of a file.'''
@@ -451,8 +458,9 @@ def pwd(self):
451
458
452
459
def quit (self ):
453
460
'''Quit, and close the connection.'''
454
- self .voidcmd ('QUIT' )
461
+ resp = self .voidcmd ('QUIT' )
455
462
self .close ()
463
+ return resp
456
464
457
465
def close (self ):
458
466
'''Close the connection without assuming anything about it.'''
@@ -495,7 +503,6 @@ def parse227(resp):
495
503
host = string .join (numbers [:4 ], '.' )
496
504
port = (string .atoi (numbers [4 ]) << 8 ) + string .atoi (numbers [5 ])
497
505
return host , port
498
- # end parse227
499
506
500
507
501
508
def parse257 (resp ):
@@ -520,10 +527,12 @@ def parse257(resp):
520
527
dirname = dirname + c
521
528
return dirname
522
529
530
+
523
531
def print_line (line ):
524
532
'''Default retrlines callback to print a line.'''
525
533
print line
526
534
535
+
527
536
def ftpcp (source , sourcename , target , targetname = '' , type = 'I' ):
528
537
'''Copy file from one FTP-instance to another.'''
529
538
if not targetname : targetname = sourcename
0 commit comments