Plan 9 from Bell Labs’s /usr/web/sources/contrib/bichued/root/sys/lib/python/ftplib.pyc

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


�
��c@s�dZddkZddkZy5ddkZeZ[ddklZee_[Wnej
oddkZnXddgZdZdZ	de
fd	��YZd
efd��YZdefd
��YZ
defd��YZdefd��YZeeieefZdZdfd��YZead�Zead�Zd�Zd�Zd�Zddd�Zdfd��YZd�Z e!djoe �ndS(sSAn 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
i�N(tgetfqdntFTPtNetrciitErrorcBseZRS((t__name__t
__module__(((s/sys/lib/python/ftplib.pyR:sterror_replycBseZRS((RR(((s/sys/lib/python/ftplib.pyR;st
error_tempcBseZRS((RR(((s/sys/lib/python/ftplib.pyR<st
error_permcBseZRS((RR(((s/sys/lib/python/ftplib.pyR=sterror_protocBseZRS((RR(((s/sys/lib/python/ftplib.pyR	>ss
cBs�eZdZdZdZeZd+Zd+Z	d+Z
dZddddd�Zddd�Z
d�Zd�ZeZd�Zd	�Zd
�Zd�Zd�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd+d�Z d+d�Z!dddd�Z"dd+d�Z#d+d�Z$dd�Z%d�Z&d�Z'd �Z(d!�Z)d"�Z*d#�Z+d$�Z,d%�Z-d&�Z.d'�Z/d(�Z0d)�Z1d*�Z2RS(,scAn FTP client class.

    To create a connection, call the class using these argument:
            host, user, passwd, acct
    These are all strings, and have default value ''.
    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.
iticCs:|o/|i|�|o|i|||�q6ndS(N(tconnecttlogin(tselfthosttusertpasswdtacct((s/sys/lib/python/ftplib.pyt__init__is
c
Cs,|o
||_n|o
||_nd}x�ti|i|idti�D]�}|\}}}}}	y,ti|||�|_|ii|	�Wn?tij
o0}|io|ii�nd|_qPnXPqPW|ipti|�n||_
|iid�|_|i
�|_|iS(s�Connect to host.  Arguments are:
        - host: hostname to connect to (string, default previous host)
        - port: port to connect to (integer, default previous port)s!getaddrinfo returns an empty listitrbN(RtporttsockettgetaddrinfotSOCK_STREAMtsockRterrortclosetNonetaftmakefiletfiletgetresptwelcome(
R
RRtmsgtresRtsocktypetprotot	canonnametsa((s/sys/lib/python/ftplib.pyRns*"
	
	cCs*|iodG|i|i�GHn|iS(s`Get the welcome message from the server.
        (this is read and squirreled away by connect())s	*welcome*(t	debuggingtsanitizeR (R
((s/sys/lib/python/ftplib.pyt
getwelcome�s
cCs
||_dS(s�Set 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(R'(R
tlevel((s/sys/lib/python/ftplib.pytset_debuglevel�scCs
||_dS(s�Use 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(t
passiveserver(R
tval((s/sys/lib/python/ftplib.pytset_pasv�scCs�|d djp|d djobt|�}x1|djo#||ddjo|d}q1W|d d|d||}nt|�S(Nispass sPASS is
t*(tlentrepr(R
tsti((s/sys/lib/python/ftplib.pyR(�s"%"cCsD|t}|idjodG|i|�GHn|ii|�dS(Nis*put*(tCRLFR'R(Rtsendall(R
tline((s/sys/lib/python/ftplib.pytputline�s
&cCs1|iodG|i|�GHn|i|�dS(Ns*cmd*(R'R(R7(R
R6((s/sys/lib/python/ftplib.pytputcmd�s cCs�|ii�}|idjodG|i|�GHn|p
t�n|dtjo|d }n |dtjo|d }n|S(Nis*get*i�i�(RtreadlineR'R(tEOFErrorR4(R
R6((s/sys/lib/python/ftplib.pytgetline�scCs||i�}|dd!djoX|d }xK|i�}|d|}|d |jo|dd!djoPq-q-n|S(Niit-s
(R;(R
R6tcodetnextline((s/sys/lib/python/ftplib.pytgetmultiline�s
cCs�|i�}|iodG|i|�GHn|d |_|d }|d	jo|Sn|djo
t|�n|djo
t|�nt|�dS(
Ns*resp*iit1t2t3t4t5(R@RARB(R?R'R(tlastrespRRR	(R
tresptc((s/sys/lib/python/ftplib.pyR�s 






cCs.|i�}|ddjo
t|�n|S(s%Expect a response beginning with '2'.iRA(RR(R
RF((s/sys/lib/python/ftplib.pytvoidresp�s
cCsqdt}|idjodG|i|�GHn|ii|t�|i�}|d djo
t|�ndS(	s�Abort 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.tABORis*put urgent*it426t226N(RJRK(R4R'R(RR5tMSG_OOBR?R	(R
R6RF((s/sys/lib/python/ftplib.pytabort�s
&cCs|i|�|i�S(s'Send a command and return the response.(R8R(R
tcmd((s/sys/lib/python/ftplib.pytsendcmd�s
cCs|i|�|i�S(s8Send a command and expect a response beginning with '2'.(R8RH(R
RN((s/sys/lib/python/ftplib.pytvoidcmd�s
cCsY|id�}t|d�t|d�g}||}ddi|�}|i|�S(sUSend a PORT command with the current host and the given
        port number.
        t.isPORT t,(tsplitR1tjoinRP(R
RRthbytestpbytestbytesRN((s/sys/lib/python/ftplib.pytsendport�s
 
cCs�d}|itijo
d}n|itijo
d}n|djo
td�ndt|�|t|�dg}ddi|�}|i|�S(sDSend a EPRT command with the current host and the given port number.iiisunsupported address familyR
sEPRT t|(RRtAF_INETtAF_INET6R	R1RTRP(R
RRRtfieldsRN((s/sys/lib/python/ftplib.pytsendeprts



!cCs9d}d}x�tidd|itidti�D]{}|\}}}}}y&ti|||�}|i|�Wn6tij
o'}|o|i�nd}q4nXPq4W|pti|�n|i	d�|i
�d}	|ii
�d}
|itijo|i
|
|	�}n|i|
|	�}|S(s3Create a new socket and send a PORT command for it.s!getaddrinfo returns an empty listiiN(RRRRRt
AI_PASSIVEtbindRRtlistentgetsocknameRRZRXR](R
R!RR"RR#R$R%R&RRRF((s/sys/lib/python/ftplib.pytmakeports.(
cCsc|itijot|id��\}}n(t|id�|ii��\}}||fS(NtPASVtEPSV(RRRZtparse227ROtparse229Rtgetpeername(R
RR((s/sys/lib/python/ftplib.pytmakepasv)s'cCs�d}|io�|i�\}}ti||dti�d\}}}}	}
ti|||�}|i|
�|dj	o|id|�n|i|�}|ddjo|i�}n|ddjo
t	|�qtn�|i
�}
|dj	o|id|�n|i|�}|ddjo|i�}n|ddjo
t	|�n|
i�\}}|d djot|�}n||fS(s�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 RESTART command.  This is essentially a server
        marker used to tell the server to skip over any data up to the
        given marker.
        isREST %sRAR@it150N(
RR,RhRRRRRORRRbtaccepttparse150(R
RNtresttsizeRRRR#R$tcanonR&tconnRFRtsockaddr((s/sys/lib/python/ftplib.pytntransfercmd0s2
.



cCs|i||�dS(s0Like ntransfercmd() but returns only the socket.i(Rq(R
RNRl((s/sys/lib/python/ftplib.pyttransfercmdbscCs�|p
d}n|p
d}n|p
d}n|djo|djo|d}n|id|�}|ddjo|id|�}n|ddjo|id	|�}n|dd
jo
t|�n|S(sLogin, default anonymous.t	anonymousR
R<s
anonymous@sUSER iRBsPASS sACCT RA(R
R<(ROR(R
RRRRF((s/sys/lib/python/ftplib.pyRfs((
i cCs^|id�|i||�}x(|i|�}|pPn||�q"|i�|i�S(sURetrieve data in binary mode.

        `cmd' is a RETR command.  `callback' is a callback function is
        called for each block.  No more than `blocksize' number of
        bytes will be read from the socket.  Optional `rest' is passed
        to transfercmd().

        A new port is created for you.  Return the response code.
        sTYPE I(RPRrtrecvRRH(R
RNtcallbackt	blocksizeRlRotdata((s/sys/lib/python/ftplib.pyt
retrbinary{s



cCs�|djo
t}n|id�}|i|�}|id�}x�|i�}|idjodGt|�GHn|pPn|dtjo|d }n |ddjo|d }n||�qG|i	�|i	�|i
�S(	sRetrieve data in line mode.
        The argument is a RETR or LIST command.
        The callback function (2nd argument) is called for each line,
        with trailing CRLF stripped.  This creates a new port for you.
        print_line() is the default callback.sTYPE ARis*retr*i�i�s
N(Rt
print_lineRORrRR9R'R1R4RRH(R
RNRuRFRotfpR6((s/sys/lib/python/ftplib.pyt	retrlines�s"#


cCs^|id�|i|�}x+|i|�}|pPn|i|�q|i�|i�S(sStore a file in binary mode.sTYPE I(RPRrtreadR5RRH(R
RNRzRvRotbuf((s/sys/lib/python/ftplib.pyt
storbinary�s

cCs�|id�|i|�}xf|i�}|pPn|dtjo-|dtjo|d }n|t}n|i|�q|i�|i�S(sStore a file in line mode.sTYPE Ai�i�(RPRrR9R4R5RRH(R
RNRzRoR}((s/sys/lib/python/ftplib.pyt	storlines�s

cCsd|}|i|�S(sSend new account name.sACCT (RP(R
tpasswordRN((s/sys/lib/python/ftplib.pyR�s
cGsBd}x|D]}|d|}q
Wg}|i||i�|S(sBReturn a list of files in a given directory (default the current).tNLSTt (R{tappend(R
targsRNtargtfiles((s/sys/lib/python/ftplib.pytnlst�scGs�d}d}|do6t|d�td�jo|d |d}}nx'|D]}|o|d|}qTqTW|i||�dS(sList 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.)tLISTi�R
R�N(RttypeR{(R
R�RNtfuncR�((s/sys/lib/python/ftplib.pytdir�s(cCsB|id|�}|ddjo
t|�n|id|�S(sRename a file.sRNFR iRBsRNTO (RORRP(R
tfromnamettonameRF((s/sys/lib/python/ftplib.pytrename�s
cCsW|id|�}|d djo|Sn(|d djo
t|�n
t|�dS(	sDelete a file.sDELE it250t200iRDN(R�R�(RORR(R
tfilenameRF((s/sys/lib/python/ftplib.pytdelete�s
cCs�|djoLy|id�SWqptj
o(}|idd djo�qUqpXn|djo
d}nd|}|i|�S(	sChange to a directory.s..tCDUPiit500R
RQsCWD (RPRR�(R
tdirnameR!RN((s/sys/lib/python/ftplib.pytcwd�s



cCsp|id|�}|d djoH|di�}yt|�SWqlttfj
ot|�SqlXndS(sRetrieve the size of a file.sSIZE it213N(ROtstriptintt
OverflowErrort
ValueErrortlong(R
R�RFR2((s/sys/lib/python/ftplib.pyRm�scCs|id|�}t|�S(s+Make a directory, return its full pathname.sMKD (ROtparse257(R
R�RF((s/sys/lib/python/ftplib.pytmkdscCs|id|�S(sRemove a directory.sRMD (RP(R
R�((s/sys/lib/python/ftplib.pytrmdscCs|id�}t|�S(s!Return current working directory.tPWD(ROR�(R
RF((s/sys/lib/python/ftplib.pytpwdscCs|id�}|i�|S(sQuit, and close the connection.tQUIT(RPR(R
RF((s/sys/lib/python/ftplib.pytquits
cCs<|io.|ii�|ii�d|_|_ndS(s8Close the connection without assuming anything about it.N(RRRR(R
((s/sys/lib/python/ftplib.pyRs


N(3RRt__doc__R'RtFTP_PORTRRRRR R,RRR)R+tdebugR.R(R7R8R;R?RRHRMRORPRXR]RbRhRqRrRRxR{R~RRR�R�R�R�R�RmR�R�R�R�R(((s/sys/lib/python/ftplib.pyRKs^								
		
					
	
		2							
	
					cCs�|d djo
t|�ntdjo%ddk}|id|i�anti|�}|pdSn|id�}yt|�SWn#t	t
fj
ot|�SnXdS(s�Parse 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.
    iRii�Ns150 .* \((\d+) bytes\)i(Rt_150_reRtretcompilet
IGNORECASEtmatchtgroupR�R�R�R�(RFR�tmR2((s/sys/lib/python/ftplib.pyRk$s

cCs�|d djo
t|�ntdjoddk}|id�anti|�}|p
t|�n|i�}di|d �}t	|d�d>t	|d	�}||fS(
s�Parse 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.it227i�Ns#(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)RQiii(
Rt_227_reRR�R�tsearchR	tgroupsRTR�(RFR�R�tnumbersRR((s/sys/lib/python/ftplib.pyRe;s


"cCs|d djo
t|�n|id�}|djo
t|�n|id|d�}|djo
t|�n||d||djo
t|�n||d|!i||d�}t|�djo
t|�n|d}t|d�}||fS(s�Parse the '229' response for a EPSV request.
    Raises error_proto if it does not contain '(|||port|)'
    Return ('host.addr.as.numbers', port#) tuple.it229t(it)ii(RtfindR	RSR0R�(RFtpeertlefttrighttpartsRR((s/sys/lib/python/ftplib.pyRfOs



"

cCs�|d djo
t|�n|dd!djodSnd}d}t|�}xn||jo`||}|d}|djo1||jp||djoPn|d}n||}qUW|S(s�Parse 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.it257is "R
it"(RR0(RFR�R3tnRG((s/sys/lib/python/ftplib.pyR�es 



cCs	|GHdS(s+Default retrlines callback to print a line.N((R6((s/sys/lib/python/ftplib.pyRy|sR
tIc	Cs�|p
|}nd|}|i|�|i|�t|id��\}}|i||�|id|�}|d d	jo
t�n|id|�}|d d
jo
t�n|i�|i�dS(s+Copy file from one FTP-instance to another.sTYPE RcsSTOR it125RisRETR N(R�s150(R�s150(RPReRORXR	RH(	tsourcet
sourcenamettargett
targetnameR�t
sourcehostt
sourceportttreplytsreply((s/sys/lib/python/ftplib.pytftpcp�s



cBsPeZdZdZdZdZdd�Zd�Zd�Z	d�Z
d�ZRS(s�Class to parse & provide access to 'netrc' format files.

    See the netrc(4) man page for information on the file format.

    WARNING: This class is obsolete -- use module netrc instead.

    cCs|djo=dtijo tiitidd�}qJtd�nh|_h|_t|d�}d}x�|i	�}|pPn|o!|i
�o|i|�qtn%|ot|�|i|<d}n|i
�}d}}	}
}d}d}
xC|
t|�jo/||
}|
dt|�jo||
d}nd}|djo
d}n�|djo!|o|i�}|
d}
n�|d	jo|o|}	|
d}
n||d
jo|o|}
|
d}
nT|djo|o|}|
d}
n,|djo|o|}g}d}Pn|
d}
qW|o=|	p|i|_|
p|i|_|p|i|_n|ok||ijoA|i|\}}}|	p|}	|
p|}
|p|}n|	|
|f|i|<qtqt|i�dS(
NtHOMEs.netrcs!specify file to load or set $HOMEtriitdefaulttmachineRR�taccounttmacdef(RtostenvirontpathRTtIOErrort
_Netrc__hostst_Netrc__macrostopenR9R�R�ttupleRSR0tlowert_Netrc__defusert_Netrc__defpasswdt_Netrc__defacctR(R
R�Rztin_macroR6tmacro_linest
macro_nametwordsRRRRR�R3tw1tw2tousertopasswdtoacct((s/sys/lib/python/ftplib.pyR�sx


		






cCs
|ii�S(s4Return a list of hosts mentioned in the .netrc file.(R�tkeys(R
((s/sys/lib/python/ftplib.pyt	get_hosts�scCs�|i�}d}}}||ijo|i|\}}}n|p|i}|p|i}|p|i}|||fS(s�Returns login information for the named host.

        The return value is a triple containing userid,
        password, and the accounting field.

        N(R�RR�R�R�R�(R
RRRR((s/sys/lib/python/ftplib.pytget_account�scCs
|ii�S(s)Return a list of all defined macro names.(R�R�(R
((s/sys/lib/python/ftplib.pyt
get_macros�scCs|i|S(s6Return a sequence of lines which define a named macro.(R�(R
tmacro((s/sys/lib/python/ftplib.pyt	get_macro�sN(RRR�RR�R�R�RR�R�R�R�(((s/sys/lib/python/ftplib.pyR�sA			cCsHtti�djotiGHtid�nd}d}x-tiddjo|d}tid=q>Wtidd djotidd}tid=ntid}t|�}|i|�d}}}yt	|�}Wn4t
j
o(|dj	otiid�q_nDXy|i
|�\}}}Wn#tj
otiid�nX|i|||�x�tidD]�}|d d	jo|i|d�q�|d djo:d
}	|do|	d|d}	n|i|	�}
q�|djo|i|i�q�|id
|tiid�q�W|i�dS(s�Test program.
    Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...

    -d dir
    -l list
    -p password
    iiis-ds-rR
s5Could not open account file -- using anonymous login.s$No account -- using anonymous login.s-ltCWDR�s-psRETR iN(R0tsystargvttestR�texitRRR+RR�tstderrtwriteR�tKeyErrorRR�ROR.R,RxtstdoutR�(R'trcfileRtftptuseridRRtnetrcRRNRF((s/sys/lib/python/ftplib.pyR��sN	



	!

t__main__("R�R�R�tSOCKSRRtImportErrort__all__RLR�t	ExceptionRRRRR	RR�R:t
all_errorsR4RRR�RkR�ReRfR�RyR�RR�R(((s/sys/lib/python/ftplib.pys<module>s> �					k	7


Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].