a
    ze                     @   sH  d dl Z d dlZd dlmZ g d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y   dZY n00 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rDe"  dS ))    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.9/ftplib.pyr   9       r   c                   @   s   e Zd ZdS )r   Nr   r   r   r   r   r   :   r   r   c                   @   s   e Zd ZdS )r   Nr   r   r   r   r   r   ;   r   r   c                   @   s   e Zd ZdS )r   Nr   r   r   r   r   r   <   r   r   c                   @   s   e Zd ZdS )r   Nr   r   r   r   r   r   =   r   r   
s   
c                   @   s  e Zd ZdZdZeZeZdZ	dZ
dZdZdZddddedfdd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 Z.dg fdHdIZ/dJdK Z0dLdM Z1dNdO Z2dPdQ Z3dRdS Z4dTdU Z5dVdW Z6dXdY Z7dZd[ Z8dS )dr   r    NTFutf-8encodingc                C   s6   || _ || _|| _|r2| | |r2| ||| d S N)r   source_addresstimeoutconnectlogin)selfhostuserpasswdacctr   r   r   r   r   r   __init__m   s    
zFTP.__init__c                 C   s   | S r   r   r   r   r   r   	__enter__}   s    zFTP.__enter__c              	   G   s^   | j d urZz:z|   W n ttfy.   Y n0 W | j d urZ|   n| j d urX|   0 d S r   )sockquitOSErrorEOFErrorclose)r   argsr   r   r   __exit__   s    



zFTP.__exit__c                 C   s   |dkr|| _ |dkr|| _|dkr*|| _| jd urB| jsBtd|d urP|| _td| | j | j tj| j | jf| j| jd| _	| j	j
| _| j	jd| jd| _|  | _| jS )	Nr   r   r-   z0Non-blocking socket (timeout=0) is not supportedzftplib.connectr   rr   )r   portr   
ValueErrorr   sysauditsocketcreate_connectionr&   familyafmakefiler   filegetrespwelcome)r   r   r0   r   r   r   r   r   r      s$    

zFTP.connectc                 C   s   | j rtd| | j | jS )Nz	*welcome*)	debuggingprintsanitizer;   r$   r   r   r   
getwelcome   s    zFTP.getwelcomec                 C   s
   || _ d S r   )r<   )r   levelr   r   r   set_debuglevel   s    zFTP.set_debuglevelc                 C   s
   || _ d S r   )passiveserver)r   valr   r   r   set_pasv   s    zFTP.set_pasvc                 C   sJ   |d d dv rBt |d}|d d d|d   ||d   }t|S )N   >   PASS zpass r   *)lenrstriprepr)r   sir   r   r   r>      s    $zFTP.sanitizec                 C   s`   d|v sd|v 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*)r1   r2   r3   CRLFr<   r=   r>   r&   sendallencoder   r   liner   r   r   putline   s    
zFTP.putlinec                 C   s$   | j rtd| | | | d S )Nz*cmd*)r<   r=   r>   rT   rR   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	v r|d d }|S )Nr	   got more than %d bytesz*get*)
r9   readlinemaxlinerH   r   r<   r=   r>   r)   rO   rR   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      -rN   )r[   )r   rS   codeZnextliner   r   r   getmultiline   s    zFTP.getmultilinec                 C   sp   |   }| jrtd| | |d d | _|d d }|dv rD|S |dkrTt||dkrdt|t|d S )Nz*resp*r\   r	   >   23145)r`   r<   r=   r>   Zlastrespr   r   r   )r   respcr   r   r   r:      s    zFTP.getrespc                 C   s$   |   }|d d dkr t||S )Nr	   ra   )r:   r   r   rf   r   r   r   voidresp  s    zFTP.voidrespc                 C   sT   dt  }| jdkr"td| | | j|t |  }|d d dvrPt||S )N   ABORr	   z*put urgent*r\      Z225Z226Z426)	B_CRLFr<   r=   r>   r&   rP   MSG_OOBr`   r   r   rS   rf   r   r   r   abort  s    
z	FTP.abortc                 C   s   |  | |  S r   )rU   r:   r   cmdr   r   r   sendcmd  s    
zFTP.sendcmdc                 C   s   |  | |  S r   )rU   ri   rp   r   r   r   voidcmd  s    
zFTP.voidcmdc                 C   sB   | d}t|d t|d g}|| }dd| }| |S )N.   zPORT ,)splitrJ   joinrs   )r   r   r0   ZhbytesZpbytesbytesrq   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 )Nr   r	      zunsupported address familyr   zEPRT |)r7   r4   AF_INETAF_INET6r   rJ   rx   rs   )r   r   r0   r7   fieldsrq   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	urh|
| j |S )N)r   r   r	   )r6   backlogr   )r4   create_serverr7   getsocknamer&   r}   rz   r   r   r   
settimeout)r   r&   r0   r   rf   r   r   r   makeport7  s    
zFTP.makeportc                 C   s\   | j tjkr:t| d\}}| jr*|}qT| j d }nt| d| j \}}||fS )NPASVr   ZEPSV)	r7   r4   r}   parse227rr   trust_server_pasv_ipv4_addressr&   getpeernameparse229)r   Zuntrusted_hostr0   r   r   r   r   makepasvD  s    zFTP.makepasvc           
      C   sL  d }| j r|  \}}tj||f| j| jd}zL|d urF| d|  | |}|d dkrd|  }|d dkrxt|W n   |	   Y n0 n| 
 |}|d ur| d|  | |}|d dkr|  }|d dkrt|| \}}	| jtur
|| j W d    n1 s 0    Y  |d d dkrDt|}||fS )Nr.   zREST %sr   ra   rc   r\   150)rB   r   r4   r5   r   r   rr   r:   r   r*   r   acceptr   r   parse150)
r   rq   restsizer   r0   connrf   r&   Zsockaddrr   r   r   ntransfercmdP  s>    



,zFTP.ntransfercmdc                 C   s   |  ||d S )Nr   )r   )r   rq   r   r   r   r   transfercmd  s    zFTP.transfercmdc                 C   s   |sd}|sd}|sd}|dkr0|dv r0|d }|  d| }|d dkrX|  d| }|d dkrr|  d	| }|d d
krt||S )NZ	anonymousr   >   r   r^   z
anonymous@zUSER r   rb   rF   ACCT ra   rr   r   )r   r    r!   r"   rf   r   r   r   r     s     z	FTP.loginr   c                 C   sr   |  d | ||D}||}|s(q2|| qtd urLt|trL|  W d    n1 s`0    Y  |  S NzTYPE I)rs   r   recv
_SSLSocket
isinstanceunwrapri   )r   rq   callback	blocksizer   r   datar   r   r   
retrbinary  s    


&zFTP.retrbinaryc              	   C   s"  |d u 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 urt|tr|  W d    n1 s0    Y  W d    n1 s0    Y  |  S )NTYPE Ar/   r   r	   rV   r{   z*retr*rW   rX   rN   )
print_linerr   r   r8   r   rY   rZ   rH   r   r<   r=   rJ   rO   r   r   r   ri   )r   rq   r   rf   r   fprS   r   r   r   	retrlines  s,    


FzFTP.retrlinesc                 C   s   |  d | ||R}||}|s(q@|| |r|| qtd urZt|trZ|  W d    n1 sn0    Y  |  S r   )rs   r   readrP   r   r   r   ri   )r   rq   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v rp|d d }|t }|| |r|| qtd urt	|tr|
  W d    n1 s0    Y  |  S )Nr   r	   rV   rW   rX   )rs   r   rY   rZ   rH   r   rl   rP   r   r   r   ri   )r   rq   r   r   r   r   r   r   r   	storlines  s     


&zFTP.storlinesc                 C   s   d| }|  |S )Nr   rs   )r   passwordrq   r   r   r   r"     s    zFTP.acctc                 G   s0   d}|D ]}|d|  }qg }|  ||j |S )NZNLST )r   append)r   r+   rq   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 )NLISTrX   r   r   )typer   )r   r+   rq   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 )Nz
OPTS MLST ;zMLSD %sZMLSDr   rX   =)	rr   rx   r   r   rI   rO   	partitionrw   lower)r   pathZfactsrq   linesrS   Zfacts_found_nameentryZfactkeyvaluer   r   r   mlsd;  s    
zFTP.mlsdc                 C   s0   |  d| }|d dkr"t|| d| S )NzRNFR r   rb   zRNTO )rr   r   rs   )r   ZfromnameZtonamerf   r   r   r   renameW  s    z
FTP.renamec                 C   s.   |  d| }|d d dv r"|S t|d S )NzDELE r\   >   Z250Z200r   )r   filenamerf   r   r   r   delete^  s    z
FTP.deletec              
   C   sr   |dkrTz|  dW S  tyP } z$|jd d d dkr< W Y d }~q`d }~0 0 n|dkr`d}d| }|  |S )	Nz..ZCDUPr   r\   500r   rt   zCWD )rs   r   r+   )r   dirnamemsgrq   r   r   r   cwdf  s    zFTP.cwdc                 C   s:   |  d| }|d d dkr6|dd   }t|S d S )NzSIZE r\   Z213)rr   stripint)r   r   rf   rK   r   r   r   r   s  s    zFTP.sizec                 C   s$   |  d| }|dsdS t|S )NzMKD 257r   rs   
startswithparse257)r   r   rf   r   r   r   mkd{  s    
zFTP.mkdc                 C   s   |  d| S )NzRMD r   )r   r   r   r   r   rmd  s    zFTP.rmdc                 C   s    |  d}|dsdS t|S )NZPWDr   r   r   rh   r   r   r   pwd  s    

zFTP.pwdc                 C   s   |  d}|   |S )NZQUIT)rs   r*   rh   r   r   r   r'     s    
zFTP.quitc                 C   s`   z<| j }d | _ |d ur|  W | j}d | _|d ur\|  n| j}d | _|d urZ|  0 d S r   )r9   r*   r&   )r   r9   r&   r   r   r   r*     s    

z	FTP.close)r   r   r-   N)N)N)r   r   r   )r   N)N)r   NN)N)9r   r   r   r<   r   FTP_PORTr0   MAXLINErZ   r&   r9   r;   rB   r   r   r#   r%   r,   r   r?   rA   debugrD   r>   rT   rU   r[   r`   r:   ri   ro   rr   rs   rz   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   sn   




7



#

			r   c                	       s   e Zd ZejZdddddddedf	dd fddZd fdd		Zd
d Z	dd Z
dd Zdd Zd fdd	Zdd Z  ZS )FTP_TLSr   Nr   r   c
             	      s   |d ur|d urt d|d ur0|d ur0t d|d us@|d urVdd l}|dtd || _|| _|d u r|tj| j||d}|| _	d| _
t j||||||	|
d 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keyfileFr   )r1   warningswarnDeprecationWarningr   r   sslZ_create_stdlib_contextssl_versioncontext_prot_psuperr#   )r   r   r    r!   r"   r   r   r   r   r   r   r   	__class__r   r   r#     s*    zFTP_TLS.__init__Tc                    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 )NzAlready using TLSzAUTH TLSzAUTH SSLZserver_hostnamer/   )moder   )r   r&   r   r   r1   r   ZPROTOCOL_TLSrs   r   wrap_socketr   r8   r   r9   rh   r   r   r   r     s    
zFTP_TLS.authc                 C   s0   t | jtjstd| d}| j | _|S )Nznot using TLSZCCC)r   r&   r   r   r1   rs   r   rh   r   r   r   ccc  s
    
zFTP_TLS.cccc                 C   s   |  d |  d}d| _|S )NzPBSZ 0zPROT PTrs   r   rh   r   r   r   prot_p   s    

zFTP_TLS.prot_pc                 C   s   |  d}d| _|S )NzPROT CFr   rh   r   r   r   prot_c  s    
zFTP_TLS.prot_cc                    s2   t  ||\}}| jr*| jj|| jd}||fS )Nr   )r   r   r   r   r   r   )r   rq   r   r   r   r   r   r   r     s    zFTP_TLS.ntransfercmdc                 C   s8   dt  }| j| |  }|d d dvr4t||S )Nrj   r\   rk   )rl   r&   rP   r`   r   rn   r   r   r   ro     s    zFTP_TLS.abort)r   r   r   T)N)r   r   r   r   ZPROTOCOL_TLS_CLIENTr   r   r#   r   r   r   r   r   r   ro   __classcell__r   r   r   r   r     s   !r   c                 C   s\   | d d dkrt | td u r<dd l}|d|j|jB at| }|sNd S t|dS )Nr\   r   r   z150 .* \((\d+) bytes\)r	   )	r   _150_rerecompile
IGNORECASEASCIImatchr   group)rf   r   mr   r   r   r   .  s    
r   c                 C   s   | d d dkrt | td u r6dd l}|d|jat| }|sLt| | }d|d d }t	|d d> t	|d  }||fS )	Nr\   Z227r   z#(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)rt   r]      rE   )
r   _227_rer   r   r   searchr   groupsrx   r   )rf   r   r   Znumbersr   r0   r   r   r   r   B  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 )Nr\   Z229(r   )r	   rE   )r   findr   rw   rH   r   )rf   Zpeerleftrightpartsr   r0   r   r   r   r   U  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 )Nr\   r   rE   z "r   r	   ")r   rH   )rf   r   rL   nrg   r   r   r   r   j  s     
r   c                 C   s   t |  d S r   )r=   )rS   r   r   r   r     s    r   r   Ic           	      C   s   |s|}d| }|  | | | t| d\}}||| |d| }|d d dvrdt| d| }|d d dvrt|   |  d S )NzTYPE r   zSTOR r\   >   Z125r   RETR )rs   r   rr   rz   r   ri   )	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
y   |d urtjd Y n80 z||\}}}W n  ty   tjd Y n0 |||| 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 q2|  d S )Nr{   r   r	   z-dz-rr   z5Could not open account file -- using anonymous login.z$No account -- using anonymous login.z-lZCWDr   z-pr   i   )rH   r2   argvr=   test__doc__exitnetrcr   rA   r(   stderrwriteZauthenticatorsKeyErrorr   r   rr   rD   rB   r   stdoutr'   )r  r<   Zrcfiler   ftpZuseridr!   r"   Znetrcobjr9   rq   rf   r   r   r   r    sR    	





"

r  __main__)r   r   )$r2   r4   r   __all__rm   r   r   	Exceptionr   r   r   r   r   r(   r)   r   rO   rl   r   r   ImportErrorr   r   r   r   ZSSLErrorr   r   r   r   r   r   r   r   r  r   r   r   r   r   <module>'   sL   
    ^
}

9
