usr/lib64/python2.7/ftplib.pyo000064400000104173147204446600012073 0ustar00 zfc@sdZddlZddlZy5ddlZeZ[ddlmZee_[Wnek rrddlZnXddlmZddgZdZ dZ d Z d e fd YZ d e fd YZde fdYZde fdYZde fdYZe eefZdZdfdYZyddlZWnek rZn9XdefdYZejde eeejfZeadZeadZdZ dZ!dZ"dddZ#dfd YZ$d!Z%e&d"kr e%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 iN(tgetfqdn(t_GLOBAL_DEFAULT_TIMEOUTtFTPtNetrciii tErrorcBseZRS((t__name__t __module__(((s/usr/lib64/python2.7/ftplib.pyR?st error_replycBseZRS((RR(((s/usr/lib64/python2.7/ftplib.pyR@st error_tempcBseZRS((RR(((s/usr/lib64/python2.7/ftplib.pyRAst error_permcBseZRS((RR(((s/usr/lib64/python2.7/ftplib.pyR Bst error_protocBseZRS((RR(((s/usr/lib64/python2.7/ftplib.pyR Css cBseZdZdZdZeZeZd,Z d,Z d,Z dZ eZddddedZddddZdZdZeZd Zd Zd Zd Zd ZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$d,dZ%d,dZ&ddddZ'dd,dZ(d,dZ)dd,d,dZ*d,dZ+d Z,d!Z-d"Z.d#Z/d$Z0d%Z1d&Z2d'Z3d(Z4d)Z5d*Z6d+Z7RS(-suAn 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. iticCs?||_|r;|j||r;|j|||q;ndS(N(ttimeouttconnecttlogin(tselfthosttusertpasswdtacctR ((s/usr/lib64/python2.7/ftplib.pyt__init__vs   icCs|dkr||_n|dkr0||_n|dkrH||_ntj|j|jf|j|_|jj|_|jjd|_ |j |_ |j S(sConnect to host. Arguments are: - host: hostname to connect to (string, default previous host) - port: port to connect to (integer, default previous port) R iitrb( RtportR tsockettcreate_connectiontsocktfamilytaftmakefiletfiletgetresptwelcome(RRRR ((s/usr/lib64/python2.7/ftplib.pyR ~s      $cCs(|jr!dG|j|jGHn|jS(s`Get the welcome message from the server. (this is read and squirreled away by connect())s *welcome*(t debuggingtsanitizeR(R((s/usr/lib64/python2.7/ftplib.pyt getwelcomes cCs ||_dS(sSet 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 (Rtlevel((s/usr/lib64/python2.7/ftplib.pytset_debuglevelscCs ||_dS(sUse 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(Rtval((s/usr/lib64/python2.7/ftplib.pytset_pasvscCs|d dks |d dkr~t|}x.|dkr\||ddkr\|d}q/W|d d|d||}nt|S(Nispass sPASS is t*(tlentrepr(Rtsti((s/usr/lib64/python2.7/ftplib.pyR!s   #!cCsid|ksd|kr'tdn|t}|jdkrUdG|j|GHn|jj|dS(Ns s s4an illegal newline character should not be containedis*put*(t ValueErrortCRLFR R!Rtsendall(Rtline((s/usr/lib64/python2.7/ftplib.pytputlines  cCs/|jrdG|j|GHn|j|dS(Ns*cmd*(R R!R1(RR0((s/usr/lib64/python2.7/ftplib.pytputcmds cCs|jj|jd}t||jkrDtd|jn|jdkrhdG|j|GHn|swtn|dtkr|d }n|dtkr|d }n|S(Nisgot more than %d bytess*get*ii( RtreadlinetmaxlineR)RR R!tEOFErrorR.(RR0((s/usr/lib64/python2.7/ftplib.pytgetlines   cCsx|j}|dd!dkrt|d }xH|j}|d|}|d |kr,|dd!dkr,Pq,q,Wn|S(Niit-s (R6(RR0tcodetnextline((s/usr/lib64/python2.7/ftplib.pyt getmultilines    cCs|j}|jr*dG|j|GHn|d |_|d }|d krQ|S|dkrit|n|dkrt|nt|dS( Ns*resp*iit1t2t3t4t5(R;R<R=(R:R R!tlastrespRR R (Rtresptc((s/usr/lib64/python2.7/ftplib.pyRs         cCs,|j}|d dkr(t|n|S(s%Expect a response beginning with '2'.iR<(RR(RRA((s/usr/lib64/python2.7/ftplib.pytvoidresps  cCsmdt}|jdkr.dG|j|GHn|jj|t|j}|d d krit|ndS( sAbort 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*it426t225t226N(RERFRG(R.R R!RR/tMSG_OOBR:R (RR0RA((s/usr/lib64/python2.7/ftplib.pytaborts  cCs|j||jS(s'Send a command and return the response.(R2R(Rtcmd((s/usr/lib64/python2.7/ftplib.pytsendcmds cCs|j||jS(s8Send a command and expect a response beginning with '2'.(R2RC(RRJ((s/usr/lib64/python2.7/ftplib.pytvoidcmds cCsY|jd}t|dt|dg}||}ddj|}|j|S(sUSend a PORT command with the current host and the given port number. t.isPORT t,(tsplitR*tjoinRL(RRRthbytestpbytestbytesRJ((s/usr/lib64/python2.7/ftplib.pytsendports   cCsd}|jtjkr!d}n|jtjkr<d}n|dkrTtdndt||t|dg}ddj|}|j|S(sESend an EPRT command with the current host and the given port number.iiisunsupported address familyR sEPRT t|(RRtAF_INETtAF_INET6R R*RPRL(RRRRtfieldsRJ((s/usr/lib64/python2.7/ftplib.pytsendeprts    !c Csqd}d}xtjdd|jtjdtjD]w}|\}}}}}y&tj|||}|j|Wn2tjk r}|r|jnd}q4nXPq4W|dkr|dk r|qtjdn|j d|j d} |j j d} |jtj kr9|j | | } n|j| | } |jtk rm|j|jn|S(s3Create a new socket and send a PORT command for it.is!getaddrinfo returns an empty listiN(tNoneRt getaddrinfoRt SOCK_STREAMt AI_PASSIVEtbindterrortclosetlistent getsocknameRRVRTRYR Rt settimeout( RterrRtresRtsocktypetprotot canonnametsaRRRA((s/usr/lib64/python2.7/ftplib.pytmakeports4.     cCs|jtjkrUt|jd\}}|jr?|}q||jjd}n't|jd|jj\}}||fS(s<Internal: Does the PASV or EPSV handshake -> (address, port)tPASVitEPSV( RRRVtparse227RKttrust_server_pasv_ipv4_addressRt getpeernametparse229(Rtuntrusted_hostRR((s/usr/lib64/python2.7/ftplib.pytmakepasv:s  'c Csd}|jr|j\}}tj||f|j}yn|dk r_|jd|n|j|}|ddkr|j}n|ddkrt|nWq|j qXn|j }z|dk r|jd|n|j|}|ddkr!|j}n|ddkr=t|n|j \}} |jt k rq|j |jnWd|j X|d dkrt|}n||fS(sInitiate 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. sREST %siR<R;Nit150(RZR%RrRRR RKRRR`RjtacceptRRctparse150( RRJtresttsizeRRtconnRARtsockaddr((s/usr/lib64/python2.7/ftplib.pyt ntransfercmdFs>        cCs|j||dS(s0Like ntransfercmd() but returns only the socket.i(Rz(RRJRv((s/usr/lib64/python2.7/ftplib.pyt transfercmdscCs|sd}n|sd}n|s-d}n|dkrR|d krR|d}n|jd|}|ddkr|jd|}n|ddkr|jd |}n|dd krt|n|S( sLogin, default anonymous.t anonymousR R7s anonymous@sUSER iR=sPASS sACCT R<(R R7(RKR(RRRRRA((s/usr/lib64/python2.7/ftplib.pyRs      i cCse|jd|j||}z.x'|j|}|s>Pn||q%WWd|jX|jS(sRetrieve 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. sTYPE IN(RLR{trecvR`RC(RRJtcallbackt blocksizeRvRxtdata((s/usr/lib64/python2.7/ftplib.pyt retrbinarys  cCs.|d krt}n|jd}|j|}d }z|jd}x|j|jd}t||jkrtd|jn|j dkrdGt |GHn|sPn|dt kr|d }n|dd kr|d }n||qNWWd |r|j n|j X|j S( snRetrieve data in line mode. A new port is created for you. Args: cmd: A RETR, LIST, NLST, or MLSD 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. sTYPE ARisgot more than %d bytesis*retr*iis N(RZt print_lineRKR{RR3R4R)RR R*R.R`RC(RRJR~RARxtfpR0((s/usr/lib64/python2.7/ftplib.pyt retrliness0     cCs{|jd|j||}zDx=|j|}|s>Pn|j||r%||q%q%WWd|jX|jS(s9Store 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. sTYPE IN(RLR{treadR/R`RC(RRJRRR~RvRxtbuf((s/usr/lib64/python2.7/ftplib.pyt storbinarys   cCs|jd|j|}zx|j|jd}t||jkrctd|jn|smPn|dtkr|dtkr|d }n|t}n|j||r"||q"q"WWd|jX|j S(shStore 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. sTYPE Aisgot more than %d bytesiiN( RLR{R3R4R)RR.R/R`RC(RRJRR~RxR((s/usr/lib64/python2.7/ftplib.pyt storliness$     cCsd|}|j|S(sSend new account name.sACCT (RL(RtpasswordRJ((s/usr/lib64/python2.7/ftplib.pyRs cGsBd}x|D]}|d|}q Wg}|j||j|S(sBReturn a list of files in a given directory (default the current).tNLSTt (Rtappend(RtargsRJtargtfiles((s/usr/lib64/python2.7/ftplib.pytnlsts  cGsd}d}|drJt|dtdkrJ|d |d}}nx%|D]}|rQ|d|}qQqQW|j||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.)tLISTiR RN(RZttypeR(RRRJtfuncR((s/usr/lib64/python2.7/ftplib.pytdirs& cCs@|jd|}|ddkr/t|n|jd|S(sRename a file.sRNFR iR=sRNTO (RKRRL(RtfromnamettonameRA((s/usr/lib64/python2.7/ftplib.pytrename+s cCs4|jd|}|d dkr'|St|dS(sDelete a file.sDELE it250t200N(RR(RKR(RtfilenameRA((s/usr/lib64/python2.7/ftplib.pytdelete2scCs|dkrSy|jdSWqhtk rO}|jdd dkrPqPqhXn|dkrhd}nd|}|j|S( sChange to a directory.s..tCDUPiit500R RMsCWD (RLR R(RtdirnametmsgRJ((s/usr/lib64/python2.7/ftplib.pytcwd:s     cCsi|jd|}|d dkre|dj}yt|SWqettfk rat|SXndS(sRetrieve the size of a file.sSIZE it213N(RKtstriptintt OverflowErrorR-tlong(RRRAR+((s/usr/lib64/python2.7/ftplib.pyRwGscCs|jd|}t|S(s+Make a directory, return its full pathname.sMKD (RKtparse257(RRRA((s/usr/lib64/python2.7/ftplib.pytmkdRscCs|jd|S(sRemove a directory.sRMD (RL(RR((s/usr/lib64/python2.7/ftplib.pytrmdWscCs|jd}t|S(s!Return current working directory.tPWD(RKR(RRA((s/usr/lib64/python2.7/ftplib.pytpwd[scCs|jd}|j|S(sQuit, and close the connection.tQUIT(RLR`(RRA((s/usr/lib64/python2.7/ftplib.pytquit`s cCsbz/|j}d|_|dk r.|jnWd|j}d|_|dk r]|jnXdS(s8Close the connection without assuming anything about it.N(RRZR`R(RRR((s/usr/lib64/python2.7/ftplib.pyR`fs      N(8RRt__doc__R RtFTP_PORTRtMAXLINER4RZRRRR%tFalseRnRRR R"R$tdebugR'R!R1R2R6R:RRCRIRKRLRTRYRjRrRzR{RRRRRRRRRRRRwRRRRR`(((s/usr/lib64/python2.7/ftplib.pyROsd              9  $         tFTP_TLSc BseZdZejZddddd d d ed d Zddde dZ dZ dZ dZ d dZdd d Zd d Zdd d d Zd d ZRS(sA 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 c Cs|dk r'|dk r'tdn|dk rN|dk rNtdn||_||_|dkrtj|jd|d|}n||_t|_ t j ||||||dS(Ns4context and keyfile arguments are mutually exclusives5context and certfile arguments are mutually exclusivetcertfiletkeyfile( RZR-RRtsslt_create_stdlib_contextt ssl_versiontcontextRt_prot_pRR( RRRRRRRRR tsource_address((s/usr/lib64/python2.7/ftplib.pyRs      cCs?|r)t|jtj r)|jntj||||S(N(t isinstanceRRt SSLSockettauthRR(RRRRtsecure((s/usr/lib64/python2.7/ftplib.pyRs cCst|jtjr$tdn|jtjkrH|jd}n|jd}|jj |jd|j |_|jj dd|_ |S(s2Set up secure control connection by using TLS/SSL.sAlready using TLSsAUTH TLSsAUTH SSLtserver_hostnametmodeR( RRRRR-RtPROTOCOL_SSLv23RLRt wrap_socketRRR(RRA((s/usr/lib64/python2.7/ftplib.pyRscCs)|jd|jd}t|_|S(sSet up secure data connection.sPBSZ 0sPROT P(RLtTrueR(RRA((s/usr/lib64/python2.7/ftplib.pytprot_ps  cCs|jd}t|_|S(s"Set up clear text data connection.sPROT C(RLRR(RRA((s/usr/lib64/python2.7/ftplib.pytprot_cs cCsLtj|||\}}|jrB|jj|d|j}n||fS(NR(RRzRRRR(RRJRvRxRw((s/usr/lib64/python2.7/ftplib.pyRzs  i cCs|jd|j||}zMx'|j|}|s>Pn||q%Wt|tjrk|jnWd|jX|jS(NsTYPE I( RLR{R}RRRtunwrapR`RC(RRJR~RRvRxR((s/usr/lib64/python2.7/ftplib.pyRs  cCs>|dkrt}n|jd}|j|}|jd}zx|j|jd}t||jkrtd|jn|j dkrdGt |GHn|sPn|dt kr|d }n|dd kr|d }n||qHWt |t jr|jnWd|j|jX|jS( NsTYPE ARisgot more than %d bytesis*retr*iis (RZRRKR{RR3R4R)RR R*R.RRRRR`RC(RRJR~RARxRR0((s/usr/lib64/python2.7/ftplib.pyRs0     cCs|jd|j||}zcx=|j|}|s>Pn|j||r%||q%q%Wt|tjr|jnWd|jX|j S(NsTYPE I( RLR{RR/RRRRR`RC(RRJRRR~RvRxR((s/usr/lib64/python2.7/ftplib.pyR s   cCs|jd|j|}zx|j|jd}t||jkrctd|jn|smPn|dtkr|dtkr|d }n|t}n|j||r"||q"q"Wt|t j r|j nWd|j X|j S(NsTYPE Aisgot more than %d bytesii(RLR{R3R4R)RR.R/RRRRR`RC(RRJRR~RxR((s/usr/lib64/python2.7/ftplib.pyRs(     N(RRRRRRRZRRRRRRRRzRRRR(((s/usr/lib64/python2.7/ftplib.pyRxs      cCs|d dkrt|ntdkrLddl}|jd|jantj|}|sedS|jd}yt|SWnt t fk rt |SXdS(sParse 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. iRsiNs150 .* \((\d+) bytes\)i( Rt_150_reRZtretcompilet IGNORECASEtmatchtgroupRRR-R(RARtmR+((s/usr/lib64/python2.7/ftplib.pyRu4s   cCs|d dkrt|ntdkrFddl}|jdantj|}|sgt|n|j}dj|d }t |dd>t |d }||fS( sParse 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.it227iNs#(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)RMiii( Rt_227_reRZRRtsearchR tgroupsRPR(RARRtnumbersRR((s/usr/lib64/python2.7/ftplib.pyRmKs     "cCs|d dkrt|n|jd}|dkrCt|n|jd|d}|dkrqt|n||d||dkrt|n||d|!j||d}t|dkrt|n|d}t|d}||fS(sParse the '229' response for an EPSV request. Raises error_proto if it does not contain '(|||port|)' Return ('host.addr.as.numbers', port#) tuple.it229t(it)ii(RtfindR ROR)R(RAtpeertlefttrighttpartsRR((s/usr/lib64/python2.7/ftplib.pyRp_s      "  cCs|d dkrt|n|dd!dkr3dSd}d}t|}xg||kr||}|d}|dkr||ks||dkrPn|d}n||}qNW|S(sParse 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"(RR)(RARR,tnRB((s/usr/lib64/python2.7/ftplib.pyRus       cCs |GHdS(s+Default retrlines callback to print a line.N((R0((s/usr/lib64/python2.7/ftplib.pyRsR tIc Cs|s|}nd|}|j||j|t|jd\}}|j|||jd|}|d d krtn|jd|}|d d krtn|j|jdS( s+Copy file from one FTP-instance to another.sTYPE RksSTOR it125RssRETR N(RRs(RRs(RLRmRKRTR RC( tsourcet sourcenamettargett targetnameRt sourcehostt sourceportttreplytsreply((s/usr/lib64/python2.7/ftplib.pytftpcps       cBsPeZdZdZdZdZddZdZdZ dZ dZ RS(sClass 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|dkrFdtjkr:tjjtjdd}qFtdni|_i|_t|d}d}x|j |j d}t ||j krt d|j n|sPn|r|j r|j|qpn"|rt||j|sZ      &       m 7