
    F\h                        S r SSKrSSKrSSKJr  / SQrSrSrSr " S S	\5      r	 " S
 S\	5      r
 " S S\	5      r " S S\	5      r " S S\	5      r\	\\4rSrSr " S S5      r SSKr\R*                  r " S S\5      r\R1                  S5        \	\\\R2                  4rSqS rSqS rS rS r S r!S S jr"S r#\$S:X  a  \#" 5         gg! \ a    Sr N4f = f)!aS  An FTP client class and some helper functions.

Based on RFC 959: File Transfer Protocol (FTP), by J. Postel and J. Reynolds

Example:

>>> from ftplib import FTP
>>> ftp = FTP('ftp.python.org') # connect to host, default port
>>> ftp.login() # default, i.e.: user anonymous, passwd anonymous@
'230 Guest login ok, access restrictions apply.'
>>> ftp.retrlines('LIST') # list directory contents
total 9
drwxr-xr-x   8 root     wheel        1024 Jan  3  1994 .
drwxr-xr-x   8 root     wheel        1024 Jan  3  1994 ..
drwxr-xr-x   2 root     wheel        1024 Jan  3  1994 bin
drwxr-xr-x   2 root     wheel        1024 Jan  3  1994 etc
d-wxrwxr-x   2 ftp      wheel        1024 Sep  5 13:43 incoming
drwxr-xr-x   2 root     wheel        1024 Nov 17  1993 lib
drwxr-xr-x   6 1094     wheel        1024 Sep 13 19:07 pub
drwxr-xr-x   3 root     wheel        1024 Jan  3  1994 usr
-rw-r--r--   1 root     root          312 Aug  1  1994 welcome.msg
'226 Transfer complete.'
>>> ftp.quit()
'221 Goodbye.'
>>>

A nice test that reveals some of the network dialogue would be:
python ftplib.py -d localhost -l -p -l
    N)_GLOBAL_DEFAULT_TIMEOUT)FTPerror_reply
error_temp
error_permerror_proto
all_errors          c                       \ rS rSrSrg)Error9    N__name__
__module____qualname____firstlineno____static_attributes__r       /usr/lib/python3.13/ftplib.pyr   r   9   s    r   r   c                       \ rS rSrSrg)r   :   r   Nr   r   r   r   r   r   :       $r   r   c                       \ rS rSrSrg)r   ;   r   Nr   r   r   r   r   r   ;       r   r   c                       \ rS rSrSrg)r   <   r   Nr   r   r   r   r   r   <   r   r   r   c                       \ rS rSrSrg)r   =   r   Nr   r   r   r   r   r   =   r   r   r   
s   
c                   v   \ rS rSrSrSrSr\r\	r
SrSrSrSrSrSSSS\S4SS	.S
 jjrS rS rS4S jrS rS r\rS rS rS rS rS rS rS rS rS r S r!S r"S r#S r$S r%S r&S5S jr'S5S  jr(S6S! jr)S7S" jr*S5S# jr+S8S$ jr,S5S% jr-S& r.S' r/S( r0S/ 4S) jr1S* r2S+ r3S, r4S- r5S. r6S/ r7S0 r8S1 r9S2 r:S3r;g)9r   J   u  An FTP client class.

To create a connection, call the class using these arguments:
        host, user, passwd, acct, timeout, source_address, encoding

The first four arguments are all strings, and have default value ''.
The parameter ´timeout´ must be numeric and defaults to None if not
passed, meaning that no timeout will be set on any ftp socket(s).
If a timeout is passed, then this is now the default timeout for all ftp
socket operations for this instance.
The last parameter is the encoding of filenames, which defaults to utf-8.

Then use self.connect() with optional host and port argument.

To download a file, use ftp.retrlines('RETR ' + filename),
or ftp.retrbinary() with slightly different arguments.
To upload a file, use ftp.storlines() or ftp.storbinary(),
which have an open file as argument (see their definitions
below for details).
The download/upload functions first issue appropriate TYPE
and PORT or PASV commands.
r    NTFutf-8encodingc                    Xpl         X`l        XPl        U(       a,  U R                  U5        U(       a  U R	                  X#U5        ggg)zInitialization method (called by class instantiation).
Initialize host to localhost, port to standard ftp port.
Optional arguments are host (for connect()),
and user, passwd, acct (for login()).
N)r)   source_addresstimeoutconnectlogin)selfhostuserpasswdacctr,   r+   r)   s           r   __init__FTP.__init__m   s>     !,LL

4.  r   c                     U $ Nr   r/   s    r   	__enter__FTP.__enter__}   s    r   c                     U R                   b0   U R                  5         U R                   b  U R	                  5         g g g ! [        [        4 a     N2f = f! U R                   b  U R	                  5         f f = fr7   )sockquitOSErrorEOFErrorclose)r/   argss     r   __exit__FTP.__exit__   sm    99 !		 99(JJL ) ! X&  99(JJL )s!   ? AA AA  A5c                 Z   US:w  a  Xl         US:  a  X l        US:w  a  X0l        U R                  b  U R                  (       d  [        S5      eUb  X@l        [
        R                  " SX R                   U R                  5        [        R                  " U R                   U R                  4U R                  U R                  S9U l	        U R                  R                  U l        U R                  R                  SU R                  S9U l        U R                  5       U l        U R                   $ )	aB  Connect to host.  Arguments are:
- host: hostname to connect to (string, default previous host)
- port: port to connect to (integer, default previous port)
- timeout: the timeout to set against the ftp socket(s)
- source_address: a 2-tuple (host, port) for the socket to bind
  to as its source address before connecting.
r&   r   z0Non-blocking socket (timeout=0) is not supportedzftplib.connectr+   rr(   )r0   portr,   
ValueErrorr+   sysauditsocketcreate_connectionr<   familyafmakefiler)   filegetrespwelcome)r/   r0   rH   r,   r+   s        r   r-   FTP.connect   s     2:I!8Id?"L<<#DLLOPP%"0		"D))TYY?,,dii-CT\\<@<O<OQ	))""II&&sT]]&C	||~||r   c                     U R                   (       a%  [        SU R                  U R                  5      5        U R                  $ )zXGet the welcome message from the server.
(this is read and squirreled away by connect())z	*welcome*)	debuggingprintsanitizerS   r8   s    r   
getwelcomeFTP.getwelcome   s,     >>+t}}T\\:;||r   c                     Xl         g)zSet the debugging level.
The required argument level means:
0: no debugging output (default)
1: print commands and responses but not body text etc.
2: also print raw lines read and sent before stripping CR/LFN)rV   )r/   levels     r   set_debuglevelFTP.set_debuglevel   s	     r   c                     Xl         g)zUse passive or active mode for data transfers.
With a false argument, use the normal PORT mode,
With a true argument, use the PASV command.N)passiveserver)r/   vals     r   set_pasvFTP.set_pasv   s
     !r   c                     US S S;   a-  [        UR                  S5      5      nUS S SUS-
  -  -   XS  -   n[        U5      $ )N   >   PASS pass r#   *)lenrstriprepr)r/   sis      r   rX   FTP.sanitize   sN    Ra5&&AHHV$%A"1QqS	!AbE)AAwr   c                 0   SU;   d  SU;   a  [        S5      e[        R                  " SX5        U[        -   nU R                  S:  a  [        SU R                  U5      5        U R                  R                  UR                  U R                  5      5        g )N
z4an illegal newline character should not be containedzftplib.sendcmdr
   z*put*)rI   rJ   rK   CRLFrV   rW   rX   r<   sendallencoder)   r/   lines     r   putlineFTP.putline   sq    4<44<STT		"D/d{>>A'4==./		$++dmm45r   c                 ~    U R                   (       a  [        SU R                  U5      5        U R                  U5        g )Nz*cmd*)rV   rW   rX   rw   ru   s     r   putcmd
FTP.putcmd   s'    >>5$--*=>Tr   c                 t   U R                   R                  U R                  S-   5      n[        U5      U R                  :  a  [	        SU R                  -  5      eU R
                  S:  a  [        SU R                  U5      5        U(       d  [        eUSS  [        :X  a  US S nU$ USS  [        ;   a  US S nU$ )Nr
   got more than %d bytesz*get*)
rQ   readlinemaxlineri   r   rV   rW   rX   r?   rr   ru   s     r   getlineFTP.getline   s    yy!!$,,"23t9t||#04<<?@@>>A'4==./N99D  "#Y$9Dr   c                     U R                  5       nUSS S:X  a5  US S n U R                  5       nUSU-   -   nUS S U:X  a  USS S:w  a   U$ M/  U$ )N      -rq   )r   )r/   rv   codenextlines       r   getmultilineFTP.getmultiline   sr    ||~!98D<<>th/BQ<4' 1,  r   c                    U R                  5       nU R                  (       a  [        SU R                  U5      5        US S U l        US S nUS;   a  U$ US:X  a  [        U5      eUS:X  a  [        U5      e[        U5      e)Nz*resp*r   r
   >   12345)r   rV   rW   rX   lastrespr   r   r   )r/   respcs      r   rR   FTP.getresp   s~      ">>(DMM$/0Ra!HK8T""8T""$r   c                 N    U R                  5       nUSS S:w  a  [        U5      eU$ )z%Expect a response beginning with '2'.Nr
   r   )rR   r   r/   r   s     r   voidrespFTP.voidresp  s*    ||~8s?d##r   c                     S[         -   nU R                  S:  a  [        SU R                  U5      5        U R                  R                  U[        5        U R                  5       nUSS S;  a  [        U5      eU$ )zAbort a file transfer.  Uses out-of-band data.
This does not follow the procedure from the RFC to send Telnet
IP and Synch; that doesn't seem to work with the servers I've
tried.  Instead, just send the ABOR command as OOB data.   ABORr
   z*put urgent*Nr      225226426)	B_CRLFrV   rW   rX   r<   rs   MSG_OOBr   r   r/   rv   r   s      r   abort	FTP.abort  sn    
 >>A.$--"56		$(  "800d##r   c                 D    U R                  U5        U R                  5       $ )z'Send a command and return the response.)rz   rR   r/   cmds     r   sendcmdFTP.sendcmd  s    C||~r   c                 D    U R                  U5        U R                  5       $ )z8Send a command and expect a response beginning with '2'.)rz   r   r   s     r   voidcmdFTP.voidcmd  s    C}}r   c                     UR                  S5      n[        US-  5      [        US-  5      /nX4-   nSSR                  U5      -   nU R                  U5      $ )zESend a PORT command with the current host and the given
port number.
.   zPORT ,)splitrk   joinr   )r/   r0   rH   hbytespbytesbytesr   s          r   sendportFTP.sendport   sS     CtSy/4S>2'||C  r   c                 $   SnU R                   [        R                  :X  a  SnU R                   [        R                  :X  a  SnUS:X  a  [	        S5      eS[        U5      U[        U5      S/nSSR                  U5      -   nU R                  U5      $ )zESend an EPRT command with the current host and the given port number.r   r
      zunsupported address familyr&   zEPRT |)rO   rL   AF_INETAF_INET6r   rk   r   r   )r/   r0   rH   rO   fieldsr   s         r   sendeprtFTP.sendeprt*  s~    77fnn$B77foo%B7:;;d2hd4j"5((||C  r   c                    [         R                  " SU R                  SS9nUR                  5       S   nU R                  R                  5       S   nU R                  [         R
                  :X  a  U R                  X25      nOU R                  X25      nU R                  [        La  UR                  U R                  5        U$ )z3Create a new socket and send a PORT command for it.)r&   r   r
   )rN   backlogr   )rL   create_serverrO   getsocknamer<   r   r   r   r,   r   
settimeout)r/   r<   rH   r0   r   s        r   makeportFTP.makeport7  s    ##GDGGQG!!$yy$$&q)77fnn$==,D==,D<<66OODLL)r   c                 T   U R                   [        R                  :X  aS  [        U R	                  S5      5      u  pU R
                  (       a  UnX24$ U R                  R                  5       S   n X24$ [        U R	                  S5      U R                  R                  5       5      u  p2X24$ )z<Internal: Does the PASV or EPSV handshake -> (address, port)PASVr   EPSV)	rO   rL   r   parse227r   trust_server_pasv_ipv4_addressr<   getpeernameparse229)r/   untrusted_hostrH   r0   s       r   makepasvFTP.makepasvD  s    77fnn$#+DLL,@#A N22%
 z yy,,.q1 z "$,,v"6		8M8M8OPJDzr   c                 *   SnU R                   (       a  U R                  5       u  pE[        R                  " XE4U R                  U R
                  S9n Ub  U R                  SU-  5        U R                  U5      nUS   S:X  a  U R                  5       nUS   S:w  a  [        U5      eOU R                  5        nUb  U R                  SU-  5        U R                  U5      nUS   S:X  a  U R                  5       nUS   S:w  a  [        U5      eUR                  5       u  piU R                  [        La  UR                  U R                  5        SSS5        WSS S:X  a  [        U5      nWU4$ !   UR                  5         e = f! , (       d  f       N<= f)	aN  Initiate a transfer over the data connection.

If the transfer is active, send a port command and the
transfer command, and accept the connection.  If the server is
passive, send a pasv command, connect to it, and start the
transfer command.  Either way, return the socket for the
connection and the expected size of the transfer.  The
expected size may be None if it could not be determined.

Optional `rest' argument can be a string that is sent as the
argument to a REST command.  This is essentially a server
marker used to tell the server to skip over any data up to the
given marker.
NrF   zREST %sr   r   r   r   150)r`   r   rL   rM   r,   r+   r   rR   r   r@   r   acceptr   r   parse150)
r/   r   restsizer0   rH   connr   r<   sockaddrs
             r   ntransfercmdFTP.ntransfercmdP  si    JD++TL$,,;?;N;NPD#LLT!12||C( 7c><<>D7c>%d++ " D#LLT!12||C(7c><<>D7c>%d++!%<<'>>OODLL1 ! 8uD>DTz'

 s   AE. 8BF.F
Fc                 *    U R                  X5      S   $ )z0Like ntransfercmd() but returns only the socket.r   )r   )r/   r   r   s      r   transfercmdFTP.transfercmd  s      +A..r   c                 "   U(       d  SnU(       d  SnU(       d  SnUS:X  a  US;   a  US-   nU R                  SU-   5      nUS   S:X  a  U R                  SU-   5      nUS   S:X  a  U R                  S	U-   5      nUS   S
:w  a  [        U5      eU$ )zLogin, default anonymous.	anonymousr&   >   r&   r   z
anonymous@zUSER r   r   rf   ACCT r   r   r   )r/   r1   r2   r3   r   s        r   r.   	FTP.login  s    DFD;6Y#6 l*F||GdN+7c><<& 01D7c><<$/D7c>d##r   c                    U R                  S5        U R                  X5       nUR                  U5      =n(       a"  U" U5        UR                  U5      =n(       a  M"  [        b%  [	        U[        5      (       a  UR                  5         SSS5        U R                  5       $ ! , (       d  f       U R                  5       $ = f)av  Retrieve data in binary mode.  A new port is created for you.

Args:
  cmd: A RETR command.
  callback: A single parameter callable to be called on each
            block of data read.
  blocksize: The maximum number of bytes to read from the
             socket at one time.  [default: 8192]
  rest: Passed to transfercmd().  [default: None]

Returns:
  The response code.
TYPE IN)r   r   recv
_SSLSocket
isinstanceunwrapr   )r/   r   callback	blocksizer   r   datas          r   
retrbinaryFTP.retrbinary  s     	Xc(D))I..$. ))I..$. %*T:*F*F ) }} )( }}s   9B",B""
B?c                    Uc  [         nU R                  S5      nU R                  U5       nUR                  SU R                  S9 n UR                  U R                  S-   5      n[        U5      U R                  :  a  [        SU R                  -  5      eU R                  S:  a  [        S[        U5      5        U(       d  O+US	S [        :X  a  USS	 nOUS
S S:X  a  USS
 nU" U5        M  [        b%  [        U[        5      (       a  UR                  5         SSS5        SSS5        U R!                  5       $ ! , (       d  f       N&= f! , (       d  f       U R!                  5       $ = f)a(  Retrieve data in line mode.  A new port is created for you.

Args:
  cmd: A RETR, LIST, or NLST command.
  callback: An optional single parameter callable that is called
            for each line with the trailing CRLF stripped.
            [default: print_line()]

Returns:
  The response code.
NTYPE ArG   r(   r
   r}   r   z*retr*r~   r   rq   )
print_liner   r   rP   r)   r   r   ri   r   rV   rW   rk   rr   r   r   r   r   )r/   r   r   r   r   fprv   s          r   	retrlinesFTP.retrlines  s5    !H||H%c"dsT]];r{{4<<!#34t9t||+ 84<< GHH>>A%(DJ/9$9D"#Y$&9D  %*T:*F*F! < #$ }}# <; #"$ }}s$   ECD<E<
E
	E
E*c                    U R                  S5        U R                  X5       nUR                  U5      =n(       a:  UR                  U5        U(       a  U" U5        UR                  U5      =n(       a  M:  [        b%  [        U[        5      (       a  UR                  5         SSS5        U R                  5       $ ! , (       d  f       U R                  5       $ = f)a  Store a file in binary mode.  A new port is created for you.

Args:
  cmd: A STOR command.
  fp: A file-like object with a read(num_bytes) method.
  blocksize: The maximum data size to read from fp and send over
             the connection at once.  [default: 8192]
  callback: An optional single parameter callable that is called on
            each block of data after it is sent.  [default: None]
  rest: Passed to transfercmd().  [default: None]

Returns:
  The response code.
r   N)r   r   readrs   r   r   r   r   )r/   r   r   r   r   r   r   bufs           r   
storbinaryFTP.storbinary  s     	Xc(D++#+S!SM ++#+
 %*T:*F*F ) }} )( }}s   AB:6,B::
Cc                 T   U R                  S5        U R                  U5       n UR                  U R                  S-   5      n[	        U5      U R                  :  a  [        SU R                  -  5      eU(       d  OJUSS [        :w  a  US   [        ;   a  USS nU[        -   nUR                  U5        U(       a  U" U5        M  [        b%  [        U[        5      (       a  UR                  5         SSS5        U R                  5       $ ! , (       d  f       U R                  5       $ = f)a(  Store a file in line mode.  A new port is created for you.

Args:
  cmd: A STOR command.
  fp: A file-like object with a readline() method.
  callback: An optional single parameter callable that is called on
            each line after it is sent.  [default: None]

Returns:
  The response code.
r   r
   r}   r~   Nr   )r   r   r   r   ri   r   r   rs   r   r   r   r   )r/   r   r   r   r   r   s         r   	storlinesFTP.storlines  s     	Xc"dkk$,,"23s8dll* 84<< GHHrs8v%2w&(CR#,CS!SM  %*T:*F*F #  }}! #"  }}s   CD


D'c                 .    SU-   nU R                  U5      $ )zSend new account name.r   r   )r/   passwordr   s      r   r3   FTP.acct  s     ||C  r   c                 f    SnU H  nUSU-   -   nM     / nU R                  X$R                  5        U$ )zBReturn a list of files in a given directory (default the current).NLST )r   append)r/   rA   r   argfiless        r   nlstFTP.nlst  s9    Cs#C sLL)r   c                     SnSnUSS (       a!  [        US   [        5      (       d	  USS US   p1U H  nU(       d  M  USU-   -   nM     U R                  X#5        g)zList a directory in long form.
By default list current directory to stdout.
Optional last argument is callback function; all
non-empty arguments before it are concatenated to the
LIST command.  (This *should* only be used for a pathname.)LISTNr   r  )r   strr   )r/   rA   r   funcr  s        r   dirFTP.dir&  sc     9ZR#66crDH$CsS3Y'  	s!r   c              #     #    U(       a&  U R                  SSR                  U5      -   S-   5        U(       a  SU-  nOSn/ nU R                  X4R                  5        U Hr  nUR	                  [
        5      R                  S5      u  pgn0 n	USS R                  S5       H)  n
U
R                  S5      u  pnXUR                  5       '   M+     X4v   Mt     g7f)	a  List a directory in a standardized format by using MLSD
command (RFC-3659). If path is omitted the current directory
is assumed. "facts" is a list of strings representing the type
of information desired (e.g. ["type", "size", "perm"]).

Return a generator object yielding a tuple of two elements
for every file found in path.
First element is the file name, the second one is a dictionary
including a variable number of "facts" depending on the server
and whether "facts" argument has been provided.
z
OPTS MLST ;zMLSD %sMLSDr  Nr   =)	r   r   r   r  rj   rr   	partitionr   lower)r/   pathfactsr   linesrv   facts_found_nameentryfactkeyvalues                r   mlsdFTP.mlsd5  s      LL7#=>d"CCsLL)D#';;t#4#>#>s#C KDE#CR(..s3 $s 3%*ciik" 4 - s   CCc                 z    U R                  SU-   5      nUS   S:w  a  [        U5      eU R                  SU-   5      $ )zRename a file.zRNFR r   r   zRNTO )r   r   r   )r/   fromnametonamer   s       r   rename
FTP.renameQ  s?    ||Gh./7c>d##||Gf,--r   c                 V    U R                  SU-   5      nUSS S;   a  U$ [        U5      e)zDelete a file.zDELE Nr   >   200250r   )r/   filenamer   s      r   delete
FTP.deleteX  s3    ||Gh./8~%Kd##r   c                     US:X  a   U R                  S5      $ US:X  a  SnS	U-   nU R                  U5      $ ! [         a!  nUR                  S   SS S:w  a  e  SnAN<SnAff = f)
zChange to a directory.z..CDUPr   Nr   500r&   r   zCWD )r   r   rA   )r/   dirnamemsgr   s       r   cwdFTP.cwd`  st    d?||F++ ]Gw||C    88A;r?e+ ,s   7 
A"AA"c                 z    U R                  SU-   5      nUSS S:X  a  USS R                  5       n[        U5      $ g)zRetrieve the size of a file.zSIZE Nr   213)r   stripint)r/   r(  r   rl   s       r   r   FTP.sizem  sE     ||Gh./8uQR Aq6M r   c                 n    U R                  SU-   5      nUR                  S5      (       d  g[        U5      $ )z+Make a directory, return its full pathname.zMKD 257r&   r   
startswithparse257)r/   r.  r   s      r   mkdFTP.mkdu  s3    ||FW,- u%%~r   c                 *    U R                  SU-   5      $ )zRemove a directory.zRMD r   )r/   r.  s     r   rmdFTP.rmd~  s    ||FW,--r   c                 h    U R                  S5      nUR                  S5      (       d  g[        U5      $ )z!Return current working directory.PWDr8  r&   r9  r   s     r   pwdFTP.pwd  s.    ||E" u%%~r   c                 H    U R                  S5      nU R                  5         U$ )zQuit, and close the connection.QUIT)r   r@   r   s     r   r=   FTP.quit  s    ||F#

r   c                      U R                   nSU l         Ub  UR                  5         U R                  nSU l        Ub  UR                  5         gg! U R                  nSU l        Ub  UR                  5         f f = f)z8Close the connection without assuming anything about it.N)rQ   r@   r<   )r/   rQ   r<   s      r   r@   	FTP.close  sr    		99DDI

99DDI

   99DDI

  s   &A )A9)rO   rV   r)   rQ   r0   r   r`   rH   r<   r+   r,   rS   )r&   r   rE   Nr7   )r&   r&   r&   )r   N)r   NN)<r   r   r   r   __doc__rV   r0   FTP_PORTrH   MAXLINEr   r<   rQ   rS   r`   r   r   r4   r9   rB   r-   rY   r]   debugrb   rX   rw   rz   r   r   rR   r   r   r   r   r   r   r   r   r   r   r.   r   r   r   r   r3   r  r  r  r#  r)  r0  r   r<  r?  rC  r=   r@   r   r   r   r   r   r   J   s*   . IDDGDDGM%*"R0/!/ !4 E!6$
 

!!
5n/4.!F4>!
" "  8.$!.r   r   c                   |   ^  \ rS rSrSrSS\SSS.U 4S jjjrSU 4S jjrS rS	 r	S
 r
S rSU 4S jjrS rSrU =r$ )FTP_TLSi  a  A FTP subclass which adds TLS support to FTP as described
in RFC-4217.

Connect as usual to port 21 implicitly securing the FTP control
connection before authenticating.

Securing the data connection requires user to explicitly ask
for it by calling prot_p() method.

Usage example:
>>> from ftplib import FTP_TLS
>>> ftps = FTP_TLS('ftp.python.org')
>>> ftps.login()  # login anonymously previously securing control channel
'230 Guest login ok, access restrictions apply.'
>>> ftps.prot_p()  # switch to secure data connection
'200 Protection level set to P'
>>> ftps.retrlines('LIST')  # list directory content securely
total 9
drwxr-xr-x   8 root     wheel        1024 Jan  3  1994 .
drwxr-xr-x   8 root     wheel        1024 Jan  3  1994 ..
drwxr-xr-x   2 root     wheel        1024 Jan  3  1994 bin
drwxr-xr-x   2 root     wheel        1024 Jan  3  1994 etc
d-wxrwxr-x   2 ftp      wheel        1024 Sep  5 13:43 incoming
drwxr-xr-x   2 root     wheel        1024 Nov 17  1993 lib
drwxr-xr-x   6 1094     wheel        1024 Sep 13 19:07 pub
drwxr-xr-x   3 root     wheel        1024 Jan  3  1994 usr
-rw-r--r--   1 root     root          312 Aug  1  1994 welcome.msg
'226 Transfer complete.'
>>> ftps.quit()
'221 Goodbye.'
>>>
Nr'   )contextr,   r+   r)   c          
      p   > Uc  [         R                  " 5       nXPl        SU l        [        T	U ]  XX4XgUS9  g )NFr(   )ssl_create_stdlib_contextrP  _prot_psuperr4   )
r/   r0   r1   r2   r3   rP  r,   r+   r)   	__class__s
            r   r4   FTP_TLS.__init__  s@     446"L DLGT$x  Ir   c                    > U(       a9  [        U R                  [        R                  5      (       d  U R	                  5         [
        TU ]  XU5      $ r7   )r   r<   rR  	SSLSocketauthrU  r.   )r/   r1   r2   r3   securerV  s        r   r.   FTP_TLS.login  s4    jCMMBB		7=t44r   c                    [        U R                  [        R                  5      (       a  [	        S5      eU R
                  R                  [        R                  :  a  U R                  S5      nOU R                  S5      nU R
                  R                  U R                  U R                  S9U l        U R                  R                  SU R                  S9U l        U$ )z2Set up secure control connection by using TLS/SSL.zAlready using TLSzAUTH TLSzAUTH SSLserver_hostnamerG   )moder)   )r   r<   rR  rY  rI   rP  protocolPROTOCOL_TLSr   wrap_socketr0   rP   r)   rQ   r   s     r   rZ  FTP_TLS.auth  s    $))S]]33 !455||$$(8(88||J/||J/00DII0VDI		**dmm*LDIKr   c                     [        U R                  [        R                  5      (       d  [	        S5      eU R                  S5      nU R                  R                  5       U l        U$ )z/Switch back to a clear-text control connection.znot using TLSCCC)r   r<   rR  rY  rI   r   r   r   s     r   cccFTP_TLS.ccc  sI    dii77 11<<&D		((*DIKr   c                 X    U R                  S5        U R                  S5      nSU l        U$ )zSet up secure data connection.zPBSZ 0zPROT PTr   rT  r   s     r   prot_pFTP_TLS.prot_p  s*     LL"<<)DDLKr   c                 6    U R                  S5      nSU l        U$ )z"Set up clear text data connection.zPROT CFrj  r   s     r   prot_cFTP_TLS.prot_c  s    <<)D DLKr   c                    > [         TU ]  X5      u  p4U R                  (       a$  U R                  R	                  UU R
                  S9nX44$ )Nr^  )rU  r   rT  rP  rc  r0   )r/   r   r   r   r   rV  s        r   r   FTP_TLS.ntransfercmd  sF    -c8JD||||//@D		 0 K:r   c                     S[         -   nU R                  R                  U5        U R                  5       nUS S S;  a  [	        U5      eU$ )Nr   r   r   )r   r<   rs   r   r   r   s      r   r   FTP_TLS.abort	  sK    V#DIId#$$&DBQx44!$''Kr   )rT  rP  rQ   r<   )r&   r&   r&   r&   )r&   r&   r&   Tr7   )r   r   r   r   rJ  r   r4   r.   rZ  rg  rk  rn  r   r   r   __classcell__)rV  s   @r   rO  rO    sN    	B	I $.E$(7	I 	I	5

			 			 	r   rO  c                     U SS S:w  a  [        U 5      e[        c-  SSKnUR                  SUR                  UR
                  -  5      q[        R                  U 5      nU(       d  g[        UR                  S5      5      $ )zParse the '150' response for a RETR request.
Returns the expected transfer size or None; size is not guaranteed to
be present in the 150 message.
Nr   r   r   z150 .* \((\d+) bytes\)r
   )	r   _150_rerecompile
IGNORECASEASCIImatchr5  group)r   rw  ms      r   r   r     sl    
 BQx5$**%r}}rxx'?AdAqwwqz?r   c                 T   U SS S:w  a  [        U 5      e[        c   SSKnUR                  SUR                  5      q[        R                  U 5      nU(       d  [        U 5      eUR                  5       nSR                  USS 5      n[        US   5      S-  [        US	   5      -   nXE4$ )
zParse the '227' response for a PASV request.
Raises error_proto if it does not contain '(h1,h2,h3,h4,p1,p2)'
Return ('host.addr.as.numbers', port#) tuple.Nr   227r   z#(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)r   r      re   )
r   _227_rerw  rx  rz  searchr   groupsr   r5  )r   rw  r}  numbersr0   rH   s         r   r   r   ,  s     BQx5$**CRXXNtA$hhjG88GBQK D
Oq C
O3D:r   c                    U SS S:w  a  [        U 5      eU R                  S5      nUS:  a  [        U 5      eU R                  SUS-   5      nUS:  a  [        U 5      eXS-      XS-
     :w  a  [        U 5      eXS-   U R                  XS-      5      n[	        U5      S:w  a  [        U 5      eUS   n[        US   5      nXV4$ )	zParse the '229' response for an EPSV request.
Raises error_proto if it does not contain '(|||port|)'
Return ('host.addr.as.numbers', port#) tuple.Nr   229(r   )r
   re   )r   findr   r   ri   r5  )r   peerleftrightpartsr0   rH   s          r   r   r   ?  s     BQx5$99S>Dax{4((IIc4!8$Eqy$1H~ai($% &&tF|4E
5zQ$7DuQx=D:r   c                     U SS S:w  a  [        U 5      eU SS S:w  a  gSnSn[        U 5      nX#:  a/  X   nUS-   nUS:X  a  X#:  d  X   S:w  a   U$ US-   nX-   nX#:  a  M/  U$ )	zParse the '257' response for a MKD or PWD request.
This is a response to a MKD or PWD request: a directory name.
Returns the directoryname in the 257 reply.Nr   r8  re   z "r&   r
   ")r   ri   )r   r.  rm   nr   s        r   r;  r;  T  s     BQx5$AayDG	AD	A
%GaC8vC N !A+ % Nr   c                     [        U 5        g)z+Default retrlines callback to print a line.N)rW   )rv   s    r   r   r   j  s	    	$Kr   c                    U(       d  UnSU-   nU R                  U5        UR                  U5        [        U R                  S5      5      u  pVUR                  XV5        UR                  SU-   5      nUSS S;  a  [        eU R                  SU-   5      nUSS S;  a  [        eU R                  5         UR                  5         g)z+Copy file from one FTP-instance to another.zTYPE r   zSTOR Nr   >   125r   RETR )r   r   r   r   r   r   )	source
sourcenametarget
targetnametype
sourcehost
sourceporttreplysreplys	            r   ftpcpr  o  s    
T>D
NN4
NN4%fnnV&<=J
OOJ+ ^^Gj01Fbqz'^^Gj01Fbqz'
OO
OOr   c                     [        [        R                  5      S:  a/  [        [        R
                  5        [        R                  " S5        SSKn SnSn[        R                  S   S:X  a/  US-   n[        R                  S	 [        R                  S   S:X  a  M/  [        R                  S   SS S:X  a'  [        R                  S   SS n[        R                  S	 [        R                  S   n[        U5      nUR                  U5        S=n=pg U R                  U5      n UR                  U5      u  pWnUR                  XVU5        [        R                  SS  H  n	U	SS S:X  a  UR!                  U	SS 5        OU	SS S:X  a)  Sn
U	SS (       a  U
S-   U	SS -   n
UR#                  U
5      nOU	S:X  a!  UR%                  UR&                  (       + 5        O`UR)                  SU	-   [        R*                  R,                  R.                  S5        [        R*                  R,                  R1                  5         [        R*                  R1                  5         M     UR3                  5         g! [        [        4 a    [        S[        R                  S	9   GN]f = f! [         a    Ub  [        S
[        R                  S	9   GNf = f)a[  Test program.
Usage: ftplib [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...

Options:
  -d        increase debugging level
  -r[file]  set alternate ~/.netrc file

Commands:
  -l[dir]   list directory
  -d[dir]   change the current directory
  -p        toggle passive and active mode
  file      retrieve the file and write it to stdout
r   r   Nr
   z-dz-rr&   z$No account -- using anonymous login.)rQ   z5Could not open account file -- using anonymous login.z-lCWDr  z-pr  i   )ri   rJ   argvrW   testrJ  exitnetrcr   r]   authenticatorsKeyError	TypeErrorstderrr>   r.   r  r   rb   r`   r   stdoutbufferwriteflushr=   )r  rV   rcfiler0   ftpuseridr2   r3   netrcobjrQ   r   r   s               r   r  r    sI    388}qdllIF
((1+
aK	HHQK ((1+
 xx{2A$!QRHHQK88A;D
d)Cy!FVK;;v&	K#+#:#:4#@ F& IIfd#8tGGDH"1XCABxsSy483;;s#DT\LLS.../NN7T>::,,22D:JJ##%

  HHJ% )$ 	K8szzJ	K  #Izz##s$   J #I) )(JJ%K K__main__)r&   I)%rJ  rJ   rL   r   __all__r   rK  rL  	Exceptionr   r   r   r   r   r>   r?   r	   rr   r   r   rR  rY  r   rO  r  SSLErrorImportErrorrv  r   r  r   r   r;  r   r  r  r   r   r   r   <module>r     s!  L   *  
 I  %      % 
 Wh'
 	R	 R	hu: Jk# kZ NN9(CLL9J $ &*,
,=@ zF K	  Js   &C CC