U
    k]e9                     @   sV  d Z ddlZddlZddlmZ dddddd	gZd
ZdZdZG dd deZ	G dd de	Z
G dd de	ZG dd de	ZG dd de	Ze	eefZdZdZG dd dZzddlZW n ek
r   dZY n0X ejZG dd deZed e	eeejfZdadd Zdadd Zdd Zdd Z d d! Z!d)d$d%Z"d&d' Z#e$d(krRe#  dS )*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                   @   s   e Zd ZdS )ErrorN__name__
__module____qualname__ r   r   /usr/lib/python3.8/ftplib.pyr   9   s    r   c                   @   s   e Zd ZdS )r   Nr   r   r   r   r   r   :   s    c                   @   s   e Zd ZdS )r   Nr   r   r   r   r   r   ;   s    c                   @   s   e Zd ZdS )r   Nr   r   r   r   r   r   <   s    c                   @   s   e Zd ZdS )r   Nr   r   r   r   r   r   =   s    
s   
c                   @   s  e Zd ZdZdZdZeZeZ	dZ
dZdZdZdZdZddddedfdd	Zd
d Zdd Zd\ddZdd Zdd ZeZdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Z d'd( Z!d)d* Z"d+d, Z#d-d. Z$d/d0 Z%d1d2 Z&d]d3d4Z'd^d5d6Z(d_d7d8Z)d`d:d;Z*dad<d=Z+dbd>d?Z,dcd@dAZ-dBdC Z.dDdE Z/dFdG Z0dg fdHdIZ1dJdK Z2dLdM Z3dNdO Z4dPdQ Z5dRdS Z6dTdU Z7dVdW Z8dXdY Z9dZd[ Z:dS )dr   ay  An FTP client class.

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

    The first four arguments are all strings, and have default value ''.
    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.

    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    Nr	   zlatin-1Fc                 C   s0   || _ || _|r,| | |r,| ||| d S N)source_addresstimeoutconnectlogin)selfhostuserpasswdacctr   r   r   r   r   __init__r   s    
zFTP.__init__c                 C   s   | S r   r   r   r   r   r   	__enter__{   s    zFTP.__enter__c              	   G   sN   | j d k	rJz*z|   W n ttfk
r0   Y nX W 5 | j d k	rH|   X d S r   )sockclosequitOSErrorEOFError)r   argsr   r   r   __exit__   s    


zFTP.__exit__c                 C   s   |dkr|| _ |dkr|| _|dkr*|| _|dk	r8|| _td| | j | j tj| j | jf| j| jd| _| jj	| _
| jjd| jd| _|  | _| jS )	aw  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   r)   Nzftplib.connectr   rencoding)r   portr   r   sysauditsocketcreate_connectionr"   familyafmakefiler-   filegetrespwelcome)r   r   r.   r   r   r   r   r   r      s     

zFTP.connectc                 C   s   | j rtd| | j | jS )z`Get the welcome message from the server.
        (this is read and squirreled away by connect())z	*welcome*)	debuggingprintsanitizer8   r    r   r   r   
getwelcome   s    zFTP.getwelcomec                 C   s
   || _ dS )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)r9   )r   levelr   r   r   set_debuglevel   s    zFTP.set_debuglevelc                 C   s
   || _ dS )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   valr   r   r   set_pasv   s    zFTP.set_pasvc                 C   sJ   |d d dkrBt |d}|d d d|d   ||d   }t|S )N   >   PASS pass r   *)lenrstriprepr)r   sir   r   r   r;      s    $zFTP.sanitizec                 C   s`   d|ksd|krt dtd| | |t }| jdkrHtd| | | j|	| j
 d S )N
z4an illegal newline character should not be containedzftplib.sendcmdr	   z*put*)
ValueErrorr/   r0   CRLFr9   r:   r;   r"   sendallencoder-   r   liner   r   r   putline   s    
zFTP.putlinec                 C   s$   | j rtd| | | | d S )Nz*cmd*)r9   r:   r;   rS   rQ   r   r   r   putcmd   s     z
FTP.putcmdc                 C   s   | j | jd }t|| jkr.td| j | jdkrHtd| | |sPt|dd  t	krn|d d }n|dd  t	kr|d d }|S )Nr	   got more than %d bytesz*get*)
r6   readlinemaxlinerF   r   r9   r:   r;   r&   rN   rQ   r   r   r   getline   s    
zFTP.getlinec                 C   s`   |   }|dd dkr\|d d }|   }|d|  }|d d |kr$|dd dkr$q\q$|S )N      -rL   )rZ   )r   rR   codeZnextliner   r   r   getmultiline   s    zFTP.getmultilinec                 C   sp   |   }| jrtd| | |d d | _|d d }|dkrD|S |dkrTt||dkrdt|t|d S )Nz*resp*r[   r	   >   23145)r_   r9   r:   r;   Zlastrespr   r   r   )r   respcr   r   r   r7      s    zFTP.getrespc                 C   s$   |   }|dd dkr t||S )z%Expect a response beginning with '2'.Nr	   r`   )r7   r   r   re   r   r   r   voidresp   s    zFTP.voidrespc                 C   sT   dt  }| jdkr"td| | | j|t |  }|dd dkrPt||S )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[      226426225)	B_CRLFr9   r:   r;   r"   rO   MSG_OOBr_   r   r   rR   re   r   r   r   abort  s    
z	FTP.abortc                 C   s   |  | |  S )z'Send a command and return the response.)rT   r7   r   cmdr   r   r   sendcmd  s    
zFTP.sendcmdc                 C   s   |  | |  S )z8Send a command and expect a response beginning with '2'.)rT   rh   rr   r   r   r   voidcmd  s    
zFTP.voidcmdc                 C   sB   | d}t|d t|d g}|| }dd| }| |S )zUSend a PORT command with the current host and the given
        port number.
        .   zPORT ,)splitrH   joinru   )r   r   r.   ZhbytesZpbytesbytesrs   r   r   r   sendport  s
    
zFTP.sendportc                 C   sb   d}| j tjkrd}| j tjkr$d}|dkr4tddt||t|dg}dd| }| |S )zESend an EPRT command with the current host and the given port number.r   r	      zunsupported address familyr   zEPRT |)r4   r1   AF_INETZAF_INET6r   rH   rz   ru   )r   r   r.   r4   Zfieldsrs   r   r   r   sendeprt&  s    zFTP.sendeprtc                 C   sl   t jd| jdd}| d }| j d }| jt jkrF| ||}n| ||}| jt	k	rh|
| j |S )z3Create a new socket and send a PORT command for it.)r   r   r	   )r3   Zbacklogr   )r1   Zcreate_serverr4   Zgetsocknamer"   r   r|   r   r   r   
settimeout)r   r"   r.   r   re   r   r   r   makeport3  s    
zFTP.makeportc                 C   s\   | j tjkr:t| d\}}| jr*|}qT| j d }nt| d| j \}}||fS )z<Internal: Does the PASV or EPSV handshake -> (address, port)PASVr   ZEPSV)	r4   r1   r   parse227rt   trust_server_pasv_ipv4_addressr"   Zgetpeernameparse229)r   Zuntrusted_hostr.   r   r   r   r   makepasv@  s    zFTP.makepasvc           
   	   C   s6  d}| j r|  \}}tj||f| j| jd}zL|dk	rF| d|  | |}|d dkrd|  }|d dkrxt|W n   |	   Y nX n| 
 r}|dk	r| d|  | |}|d dkr|  }|d dkrt|| \}}	| jtk	r
|| j W 5 Q R X |dd dkr.t|}||fS )	a  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.
        Nr*   zREST %sr   r`   rb   r[   150)r?   r   r1   r2   r   r   rt   r7   r   r#   r   Zacceptr   r   parse150)
r   rs   restsizer   r.   connre   r"   Zsockaddrr   r   r   ntransfercmdL  s>    



zFTP.ntransfercmdc                 C   s   |  ||d S )z0Like ntransfercmd() but returns only the socket.r   )r   )r   rs   r   r   r   r   transfercmd  s    zFTP.transfercmdc                 C   s   |sd}|sd}|sd}|dkr0|dkr0|d }|  d| }|d dkrX|  d| }|d dkrr|  d	| }|d d
krt||S )zLogin, default anonymous.Z	anonymousr   >   r   r]   z
anonymous@zUSER r   ra   rC   ACCT r`   rt   r   )r   r   r   r   re   r   r   r   r     s     z	FTP.loginr   c              	   C   s^   |  d | ||:}||}|s(q2|| qtdk	rLt|trL|  W 5 Q R X |  S )a  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)ru   r   Zrecv
_SSLSocket
isinstanceunwraprh   )r   rs   callback	blocksizer   r   datar   r   r   
retrbinary  s    


zFTP.retrbinaryc              
   C   s   |dkrt }| d}| |}|jd| jd}|| jd }t|| jkr`td| j | j	dkrxt
dt| |s~q|d	d tkr|dd	 }n|d
d dkr|dd
 }|| q4tdk	rt|tr|  W 5 Q R X W 5 Q R X |  S )ah  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 Ar+   r,   r	   rU   r}   z*retr*rV   rW   rL   )
print_linert   r   r5   r-   rX   rY   rF   r   r9   r:   rH   rN   r   r   r   rh   )r   rs   r   re   r   fprR   r   r   r   	retrlines  s,    


zFTP.retrlinesc              	   C   sl   |  d | ||H}||}|s(q@|| |r|| qtdk	rZt|trZ|  W 5 Q R X |  S )a9  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)ru   r   readrO   r   r   r   rh   )r   rs   r   r   r   r   r   bufr   r   r   
storbinary  s    



zFTP.storbinaryc              	   C   s   |  d | |}|| jd }t|| jkrBtd| j |sHq|dd tkrx|d tkrp|dd }|t }|| |r|| qtdk	rt	|tr|
  W 5 Q R X |  S )ah  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	   rU   rV   NrW   )ru   r   rX   rY   rF   r   rn   rO   r   r   r   rh   )r   rs   r   r   r   r   r   r   r   	storlines  s"    
 

zFTP.storlinesc                 C   s   d| }|  |S )zSend new account name.r   ru   )r   Zpasswordrs   r   r   r   r     s    zFTP.acctc                 G   s0   d}|D ]}|d|  }qg }|  ||j |S )zBReturn a list of files in a given directory (default the current).ZNLST )r   append)r   r'   rs   argfilesr   r   r   nlst  s    zFTP.nlstc                 G   sh   d}d}|dd r>t |d t dkr>|dd |d  }}|D ]}|rB|d|  }qB| || dS )a  List 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.)ZLISTNrW   r   r   )typer   )r   r'   rs   funcr   r   r   r   dir(  s     zFTP.dirc                 c   s   |r|  dd| d  |r*d| }nd}g }| ||j |D ]\}|td\}}}i }	|dd dD ] }
|
d\}}}||	| < qt||	fV  qDdS )	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 %sZMLSDr   NrW   =)	rt   rz   r   r   rG   rN   	partitionry   lower)r   pathZfactsrs   linesrR   Zfacts_found_nameentryZfactkeyvaluer   r   r   mlsd7  s    
zFTP.mlsdc                 C   s0   |  d| }|d dkr"t|| d| S )zRename a file.zRNFR r   ra   zRNTO )rt   r   ru   )r   ZfromnameZtonamere   r   r   r   renameS  s    z
FTP.renamec                 C   s.   |  d| }|dd dkr"|S t|dS )zDelete a file.zDELE Nr[   >   200250r   )r   filenamere   r   r   r   deleteZ  s    z
FTP.deletec              
   C   sp   |dkrRz|  dW S  tk
rN } z|jd dd dkr> W 5 d}~X Y q^X n|dkr^d}d	| }|  |S )
zChange to a directory.z..ZCDUPr   Nr[   500r   rv   zCWD )ru   r   r'   )r   dirnamemsgrs   r   r   r   cwdb  s    zFTP.cwdc                 C   s:   |  d| }|dd dkr6|dd  }t|S dS )zRetrieve the size of a file.zSIZE Nr[   Z213)rt   stripint)r   r   re   rI   r   r   r   r   o  s    zFTP.sizec                 C   s$   |  d| }|dsdS t|S )z+Make a directory, return its full pathname.zMKD 257r   ru   
startswithparse257)r   r   re   r   r   r   mkdw  s    
zFTP.mkdc                 C   s   |  d| S )zRemove a directory.zRMD r   )r   r   r   r   r   rmd  s    zFTP.rmdc                 C   s    |  d}|dsdS t|S )z!Return current working directory.ZPWDr   r   r   rg   r   r   r   pwd  s    

zFTP.pwdc                 C   s   |  d}|   |S )zQuit, and close the connection.ZQUIT)ru   r#   rg   r   r   r   r$     s    
zFTP.quitc                 C   sD   z | j}d| _|dk	r|  W 5 | j }d| _ |dk	r>|  X dS )z8Close the connection without assuming anything about it.N)r"   r#   r6   )r   r"   r6   r   r   r   r#     s    z	FTP.close)r   r   r)   N)N)N)r   r   r   )r   N)N)r   NN)N);r   r   r   __doc__r9   r   FTP_PORTr.   MAXLINErY   r"   r6   r8   r?   r-   r   r   r   r!   r(   r   r<   r>   debugrA   r;   rS   rT   rZ   r_   r7   rh   rq   rt   ru   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$   r#   r   r   r   r   r   J   sp    
	




7



#

			c                	   @   sn   e Zd ZdZejZdddddddedf	ddZdddZ	d	d
 Z
dd Zdd Zdd ZdddZdd ZdS )FTP_TLSa  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.'
        >>>
        r   Nc
              	   C   s   |d k	r|d k	rt d|d k	r0|d k	r0t d|d k	s@|d k	rVdd l}
|
dtd || _|| _|d kr|tj| j||d}|| _	d| _
t| ||||||	 d S )Nz4context and keyfile arguments are mutually exclusivez5context and certfile arguments are mutually exclusiver   zAkeyfile and certfile are deprecated, use a custom context insteadr}   )certfilekeyfileF)rM   warningswarnDeprecationWarningr   r   sslZ_create_stdlib_contextssl_versioncontext_prot_pr   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s(     zFTP_TLS.__init__Tc                 C   s*   |rt | jtjs|   t| |||S r   )r   r"   r   	SSLSocketauthr   r   )r   r   r   r   Zsecurer   r   r   r     s    zFTP_TLS.loginc                 C   sf   t | jtjrtd| jtjkr.| d}n
| d}| jj	| j| j
d| _| jjd| jd| _|S )z2Set up secure control connection by using TLS/SSL.zAlready using TLSzAUTH TLSzAUTH SSLZserver_hostnamer+   )moder-   )r   r"   r   r   rM   r   ZPROTOCOL_TLSru   r   wrap_socketr   r5   r-   r6   rg   r   r   r   r     s    

zFTP_TLS.authc                 C   s0   t | jtjstd| d}| j | _|S )z/Switch back to a clear-text control connection.znot using TLSZCCC)r   r"   r   r   rM   ru   r   rg   r   r   r   ccc  s
    
zFTP_TLS.cccc                 C   s   |  d |  d}d| _|S )zSet up secure data connection.zPBSZ 0zPROT PTru   r   rg   r   r   r   prot_p  s    

zFTP_TLS.prot_pc                 C   s   |  d}d| _|S )z"Set up clear text data connection.zPROT CFr   rg   r   r   r   prot_c  s    
zFTP_TLS.prot_cc                 C   s2   t | ||\}}| jr*| jj|| jd}||fS )Nr   )r   r   r   r   r   r   )r   rs   r   r   r   r   r   r   r     s    zFTP_TLS.ntransfercmdc                 C   s8   dt  }| j| |  }|d d dkr4t||S )Nri   r[   rj   )rn   r"   rO   r_   r   rp   r   r   r   rq     s    zFTP_TLS.abort)r   r   r   T)N)r   r   r   r   r   ZPROTOCOL_TLS_CLIENTr   r   r   r   r   r   r   r   r   rq   r   r   r   r   r     s    
  


r   c                 C   s\   | dd dkrt | tdkr<ddl}|d|j|jB at| }|sNdS t|dS )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matchr   group)re   r   mr   r   r   r   )  s     

r   c                 C   s   | dd dkrt | tdkr6ddl}|d|jat| }|sLt| | }d|dd }t	|d d> t	|d	  }||fS )
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[   Z227r   z#(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)rv   r\      rB   )
r   _227_rer   r   r   searchr   groupsrz   r   )re   r   r   Znumbersr   r.   r   r   r   r   =  s    
r   c                 C   s   | dd dkrt | | d}|dk r2t| | d|d }|dk rRt| | |d  | |d  krrt| | |d | | |d  }t|dkrt| |d }t|d }||fS )	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[   Z229(r   )r	   rB   )r   findr   ry   rF   r   )re   Zpeerleftrightpartsr   r.   r   r   r   r   Q  s     
 r   c                 C   s   | dd dkrt | | dd dkr,dS d}d}t| }||k r| | }|d }|dkrz||ks| | dkrrq|d }|| }q<|S )	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[   r   rB   z "r   r	   ")r   rF   )re   r   rJ   nrf   r   r   r   r   g  s     
r   c                 C   s   t |  dS )z+Default retrlines callback to print a line.N)r:   )rR   r   r   r   r   ~  s    r   r   Ic           	      C   s   |s|}d| }|  | | | t| d\}}||| |d| }|dd dkrdt| d| }|dd dkrt|   |  dS )z+Copy file from one FTP-instance to another.zTYPE r   zSTOR Nr[   >   r   125RETR )ru   r   rt   r|   r   rh   )	sourceZ
sourcenametargetZ
targetnamer   Z
sourcehostZ
sourceportZtreplyZsreplyr   r   r   ftpcp  s    

r   c                  C   s  t tjdk r"ttj td ddl} d}d}tjd dkrR|d }tjd= q2tjd dd dkrtjd dd }tjd= tjd }t|}|	| d } }}z| |}W n( t
k
r   |dk	rtjd Y n:X z||\}}}W n" tk
r   tjd	 Y nX |||| tjdd D ]}	|	dd d
kr`||	dd  nt|	dd dkrd}
|	dd r|
d |	dd  }
||
}n0|	dkr||j  n|d|	 tjjd q6|  dS )zTest program.
    Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...

    -d dir
    -l list
    -p password
    r}   r   Nr	   z-dz-rr   z5Could not open account file -- using anonymous login.z$No account -- using anonymous login.z-lZCWDr   z-pr   i   )rF   r/   argvr:   testr   exitnetrcr   r>   r%   stderrwriteZauthenticatorsKeyErrorr   r   rt   rA   r?   r   stdoutr$   )r   r9   Zrcfiler   ZftpZuseridr   r   Znetrcobjr6   rs   re   r   r   r   r     sV    	





 

 r   __main__)r   r   )%r   r/   r1   r   __all__ro   r   r   	Exceptionr   r   r   r   r   r%   r&   r   rN   rn   r   r   ImportErrorr   r   r   r   ZSSLErrorr   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sR   &

    Z
|

9
