42
42
import warnings
43
43
from socket import _GLOBAL_DEFAULT_TIMEOUT
44
44
45
- __all__ = ["FTP" ,"Netrc" ]
45
+ __all__ = ["FTP" , "Netrc" ]
46
46
47
47
# Magic number from <socket.h>
48
48
MSG_OOB = 0x1 # Process data out of band
@@ -184,7 +184,8 @@ def sanitize(self, s):
184
184
# Internal: send one line to the server, appending CRLF
185
185
def putline (self , line ):
186
186
line = line + CRLF
187
- if self .debugging > 1 : print ('*put*' , self .sanitize (line ))
187
+ if self .debugging > 1 :
188
+ print ('*put*' , self .sanitize (line ))
188
189
self .sock .sendall (line .encode (self .encoding ))
189
190
190
191
# Internal: send one command to the server (through putline())
@@ -198,9 +199,12 @@ def getline(self):
198
199
line = self .file .readline ()
199
200
if self .debugging > 1 :
200
201
print ('*get*' , self .sanitize (line ))
201
- if not line : raise EOFError
202
- if line [- 2 :] == CRLF : line = line [:- 2 ]
203
- elif line [- 1 :] in CRLF : line = line [:- 1 ]
202
+ if not line :
203
+ raise EOFError
204
+ if line [- 2 :] == CRLF :
205
+ line = line [:- 2 ]
206
+ elif line [- 1 :] in CRLF :
207
+ line = line [:- 1 ]
204
208
return line
205
209
206
210
# Internal: get a response from the server, which may possibly
@@ -223,7 +227,8 @@ def getmultiline(self):
223
227
# Raise various errors if the response indicates an error
224
228
def getresp (self ):
225
229
resp = self .getmultiline ()
226
- if self .debugging : print ('*resp*' , self .sanitize (resp ))
230
+ if self .debugging :
231
+ print ('*resp*' , self .sanitize (resp ))
227
232
self .lastresp = resp [:3 ]
228
233
c = resp [:1 ]
229
234
if c in {'1' , '2' , '3' }:
@@ -247,7 +252,8 @@ def abort(self):
247
252
IP and Synch; that doesn't seem to work with the servers I've
248
253
tried. Instead, just send the ABOR command as OOB data.'''
249
254
line = b'ABOR' + B_CRLF
250
- if self .debugging > 1 : print ('*put urgent*' , self .sanitize (line ))
255
+ if self .debugging > 1 :
256
+ print ('*put urgent*' , self .sanitize (line ))
251
257
self .sock .sendall (line , MSG_OOB )
252
258
resp = self .getmultiline ()
253
259
if resp [:3 ] not in {'426' , '225' , '226' }:
@@ -388,9 +394,12 @@ def transfercmd(self, cmd, rest=None):
388
394
389
395
def login (self , user = '' , passwd = '' , acct = '' ):
390
396
'''Login, default anonymous.'''
391
- if not user : user = 'anonymous'
392
- if not passwd : passwd = ''
393
- if not acct : acct = ''
397
+ if not user :
398
+ user = 'anonymous'
399
+ if not passwd :
400
+ passwd = ''
401
+ if not acct :
402
+ acct = ''
394
403
if user == 'anonymous' and passwd in {'' , '-' }:
395
404
# If there is no anonymous ftp password specified
396
405
# then we'll just use anonymous@
@@ -401,8 +410,10 @@ def login(self, user = '', passwd = '', acct = ''):
401
410
# host or country.
402
411
passwd = passwd + 'anonymous@'
403
412
resp = self .sendcmd ('USER ' + user )
404
- if resp [0 ] == '3' : resp = self .sendcmd ('PASS ' + passwd )
405
- if resp [0 ] == '3' : resp = self .sendcmd ('ACCT ' + acct )
413
+ if resp [0 ] == '3' :
414
+ resp = self .sendcmd ('PASS ' + passwd )
415
+ if resp [0 ] == '3' :
416
+ resp = self .sendcmd ('ACCT ' + acct )
406
417
if resp [0 ] != '2' :
407
418
raise error_reply (resp )
408
419
return resp
@@ -442,13 +453,15 @@ def retrlines(self, cmd, callback = None):
442
453
Returns:
443
454
The response code.
444
455
"""
445
- if callback is None : callback = print_line
456
+ if callback is None :
457
+ callback = print_line
446
458
resp = self .sendcmd ('TYPE A' )
447
459
with self .transfercmd (cmd ) as conn , \
448
460
conn .makefile ('r' , encoding = self .encoding ) as fp :
449
461
while 1 :
450
462
line = fp .readline ()
451
- if self .debugging > 2 : print ('*retr*' , repr (line ))
463
+ if self .debugging > 2 :
464
+ print ('*retr*' , repr (line ))
452
465
if not line :
453
466
break
454
467
if line [- 2 :] == CRLF :
@@ -477,9 +490,11 @@ def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
477
490
with self .transfercmd (cmd , rest ) as conn :
478
491
while 1 :
479
492
buf = fp .read (blocksize )
480
- if not buf : break
493
+ if not buf :
494
+ break
481
495
conn .sendall (buf )
482
- if callback : callback (buf )
496
+ if callback :
497
+ callback (buf )
483
498
return self .voidresp ()
484
499
485
500
def storlines (self , cmd , fp , callback = None ):
@@ -498,12 +513,14 @@ def storlines(self, cmd, fp, callback=None):
498
513
with self .transfercmd (cmd ) as conn :
499
514
while 1 :
500
515
buf = fp .readline ()
501
- if not buf : break
516
+ if not buf :
517
+ break
502
518
if buf [- 2 :] != B_CRLF :
503
519
if buf [- 1 ] in B_CRLF : buf = buf [:- 1 ]
504
520
buf = buf + B_CRLF
505
521
conn .sendall (buf )
506
- if callback : callback (buf )
522
+ if callback :
523
+ callback (buf )
507
524
return self .voidresp ()
508
525
509
526
def acct (self , password ):
@@ -768,14 +785,16 @@ def retrbinary(self, cmd, callback, blocksize=8192, rest=None):
768
785
return self .voidresp ()
769
786
770
787
def retrlines (self , cmd , callback = None ):
771
- if callback is None : callback = print_line
788
+ if callback is None :
789
+ callback = print_line
772
790
resp = self .sendcmd ('TYPE A' )
773
791
conn = self .transfercmd (cmd )
774
792
fp = conn .makefile ('r' , encoding = self .encoding )
775
793
with fp , conn :
776
794
while 1 :
777
795
line = fp .readline ()
778
- if self .debugging > 2 : print ('*retr*' , repr (line ))
796
+ if self .debugging > 2 :
797
+ print ('*retr*' , repr (line ))
779
798
if not line :
780
799
break
781
800
if line [- 2 :] == CRLF :
@@ -793,9 +812,11 @@ def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
793
812
with self .transfercmd (cmd , rest ) as conn :
794
813
while 1 :
795
814
buf = fp .read (blocksize )
796
- if not buf : break
815
+ if not buf :
816
+ break
797
817
conn .sendall (buf )
798
- if callback : callback (buf )
818
+ if callback :
819
+ callback (buf )
799
820
# shutdown ssl layer
800
821
if isinstance (conn , ssl .SSLSocket ):
801
822
conn .unwrap ()
@@ -806,12 +827,15 @@ def storlines(self, cmd, fp, callback=None):
806
827
with self .transfercmd (cmd ) as conn :
807
828
while 1 :
808
829
buf = fp .readline ()
809
- if not buf : break
830
+ if not buf :
831
+ break
810
832
if buf [- 2 :] != B_CRLF :
811
- if buf [- 1 ] in B_CRLF : buf = buf [:- 1 ]
833
+ if buf [- 1 ] in B_CRLF :
834
+ buf = buf [:- 1 ]
812
835
buf = buf + B_CRLF
813
836
conn .sendall (buf )
814
- if callback : callback (buf )
837
+ if callback :
838
+ callback (buf )
815
839
# shutdown ssl layer
816
840
if isinstance (conn , ssl .SSLSocket ):
817
841
conn .unwrap ()
@@ -924,7 +948,8 @@ def print_line(line):
924
948
925
949
def ftpcp (source , sourcename , target , targetname = '' , type = 'I' ):
926
950
'''Copy file from one FTP-instance to another.'''
927
- if not targetname : targetname = sourcename
951
+ if not targetname :
952
+ targetname = sourcename
928
953
type = 'TYPE ' + type
929
954
source .voidcmd (type )
930
955
target .voidcmd (type )
@@ -934,9 +959,11 @@ def ftpcp(source, sourcename, target, targetname = '', type = 'I'):
934
959
# transfer request.
935
960
# So: STOR before RETR, because here the target is a "user".
936
961
treply = target .sendcmd ('STOR ' + targetname )
937
- if treply [:3 ] not in {'125' , '150' }: raise error_proto # RFC 959
962
+ if treply [:3 ] not in {'125' , '150' }:
963
+ raise error_proto # RFC 959
938
964
sreply = source .sendcmd ('RETR ' + sourcename )
939
- if sreply [:3 ] not in {'125' , '150' }: raise error_proto # RFC 959
965
+ if sreply [:3 ] not in {'125' , '150' }:
966
+ raise error_proto # RFC 959
940
967
source .voidresp ()
941
968
target .voidresp ()
942
969
@@ -968,7 +995,8 @@ def __init__(self, filename=None):
968
995
in_macro = 0
969
996
while 1 :
970
997
line = fp .readline ()
971
- if not line : break
998
+ if not line :
999
+ break
972
1000
if in_macro and line .strip ():
973
1001
macro_lines .append (line )
974
1002
continue
0 commit comments