�
�f�����dZddlZddlZddlmZgd�ZdZdZdZGd�d e��Z Gd
�de ��Z
Gd�d
e ��ZGd�de ��ZGd�de ��Z
e eefZdZdZGd�d��Z ddlZejZGd�de��Ze�d��e eeejfZn
#e$rdZYnwxYwdad�Zdad�Zd�Zd�Z d�Z!d"d�Z"d �Z#e$d!kre#��dSdS)#aSAn 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��eZdZdS)�ErrorN��__name__�
__module__�__qualname__���-/opt/alt/python311/lib64/python3.11/ftplib.pyrr9��������rrc��eZdZdS)rNrrrrrr:rrrc��eZdZdS)rNrrrrrr;rrrc��eZdZdS)rNrrrrrr<rrrc��eZdZdS)rNrrrrrr=rrr�
s
c�Z�eZdZdZdZdZeZeZ dZ
dZdZdZ
dZddddedfdd�d �Zd
�Zd�Zd4d
�Zd�Zd�ZeZd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z!d�Z"d�Z#d�Z$d�Z%d5d�Z&d5d �Z'd6d!�Z(d7d#�Z)d5d$�Z*d8d%�Z+d5d&�Z,d'�Z-d(�Z.d)�Z/dgfd*�Z0d+�Z1d,�Z2d-�Z3d.�Z4d/�Z5d0�Z6d1�Z7d2�Z8d3�Z9dS)9ru�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��||_||_||_|r0|�|��|r|�|||��dSdSdS)z�Initialization 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__zFTP.__init__msl��!��
�,������� /��L�L������
/��
�
�4���.�.�.�.�.� /� /�
/�
/rc��|S�Nr�r&s r� __enter__z
FTP.__enter__}s���rc���|j�m |���n#ttf$rYnwxYw|j�|���dSdS#|j�|���wwxYwdSr-)�sock�quit�OSError�EOFError�close)r&�argss r�__exit__zFTP.__exit__�s����9� �
!�� � �������X�&�
�
�
���
�����9�(��J�J�L�L�L�L�L�)�(��4�9�(��J�J�L�L�L�L�)����
!� s!��A�2�A�2�A�A2����c���|dkr||_|dkr||_|dkr||_|j�|jstd���|�||_tjd||j|j��tj|j|jf|j|j���|_ |j j
|_|j �d|j
� ��|_|���|_|jS)
awConnect 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.
rrr8Nz0Non-blocking socket (timeout=0) is not supportedzftplib.connect�r"�rr)r'�portr#�
ValueErrorr"�sys�audit�socket�create_connectionr1�family�af�makefiler �file�getresp�welcome)r&r'r<r#r"s rr$zFTP.connect�s����2�:�:��D�I��!�8�8��D�I��d�?�?�"�D�L��<�#�D�L�#��O�P�P�P��%�"0�D��� �"�D�$�)�T�Y�?�?�?��,�d�i���-C�T�\�<@�<O�Q�Q�Q�� ��)�"����I�&�&�s�T�]�&�C�C�� ��|�|�~�~����|�rc�n�|jr(td|�|j����|jS)z`Get the welcome message from the server.
(this is read and squirreled away by connect())z *welcome*)� debugging�print�sanitizerGr.s r�
getwelcomezFTP.getwelcome�s4���>� <��+�t�}�}�T�\�:�:�;�;�;��|�rc��||_dS)z�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)rI)r&�levels r�set_debuglevelzFTP.set_debuglevel�s������rc��||_dS)z�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)�
passiveserver)r&�vals r�set_pasvzFTP.set_pasv�s��!����rc��|dd�dvr@t|�d����}|dd�d|dz
zz||d�z}t|��S)N�>�pass �PASS r�*)�len�rstrip�repr)r&�s�is rrKzFTP.sanitize�sa���R�a�R�5�&�&�&��A�H�H�V�$�$�%�%�A��"�1�"���Q�q�S� �!�A�a�b�b�E�)�A��A�w�w�rc�4�d|vsd|vrtd���tjd||��|tz}|jdkr#td|�|����|j�|� |j
����dS)N�
�
z4an illegal newline character should not be containedzftplib.sendcmdr
z*put*)r=r>r?�CRLFrIrJrKr1�sendall�encoder �r&�lines r�putlinezFTP.putline�s����4�<�<�4�4�<�<��S�T�T�T�� �"�D�$�/�/�/��d�{���>�A����'�4�=�=��.�.�/�/�/�� ���$�+�+�d�m�4�4�5�5�5�5�5rc��|jr#td|�|����|�|��dS)Nz*cmd*)rIrJrKrfrds r�putcmdz
FTP.putcmd�s=���>�>�5��$�-�-��*=�*=�>�>�>����T�����rc��|j�|jdz��}t|��|jkrt d|jz���|jdkr#t
d|�|����|st�|dd�tkr|dd�}n|dd�tvr
|dd�}|S)Nr
�got more than %d bytesz*get*������)
rE�readline�maxlinerYrrIrJrKr4rards r�getlinezFTP.getline�s����y�!�!�$�,��"2�3�3���t�9�9�t�|�#�#��0�4�<�?�@�@�@��>�A����'�4�=�=��.�.�/�/�/�� ��N�����9��������9�D�D�
�"�#�#�Y�$�
�
�����9�D��rc���|���}|dd�dkrE|dd�} |���}|d|zz}|dd�|kr|dd�dkrn�:|S)N���-r
r`)ro)r&re�code�nextlines r�getmultilinezFTP.getmultiline�s����|�|�~�~����!��9��������8�D�
��<�<�>�>���t�h��/���B�Q�B�<�4�'�'� ��1��
��,�,��
��rc�.�|���}|jr#td|�|����|dd�|_|dd�}|dvr|S|dkrt|���|dkrt
|���t|���)Nz*resp*rqr
>�1�2�3�4�5)rvrIrJrK�lastresprrr)r&�resp�cs rrFzFTP.getresp�s���� � �"�"���>� 1��(�D�M�M�$�/�/�0�0�0��R�a�R���
���!��H�������K���8�8��T�"�"�"���8�8��T�"�"�"��$���rc�h�|���}|dd�dkrt|���|S)z%Expect a response beginning with '2'.Nr
ry)rFr�r&r~s r�voidrespzFTP.voidresps3���|�|�~�~������8�s�?�?��d�#�#�#��rc��dtz}|jdkr#td|�|����|j�|t��|���}|dd�dvrt|���|S)z�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.�ABORr
z*put urgent*Nrq��225�226�426) �B_CRLFrIrJrKr1rb�MSG_OOBrvr�r&rer~s r�abortz FTP.aborts���
�����>�A����.�$�-�-��"5�"5�6�6�6�� ���$��(�(�(�� � �"�"������8�0�0�0��d�#�#�#��rc�T�|�|��|���S)z'Send a command and return the response.)rhrF�r&�cmds r�sendcmdzFTP.sendcmds"�����C�����|�|�~�~�rc�T�|�|��|���S)z8Send a command and expect a response beginning with '2'.)rhr�r�s r�voidcmdzFTP.voidcmds"�����C�����}�}���rc���|�d��}t|dz��t|dz��g}||z}dd�|��z}|�|��S)zUSend a PORT command with the current host and the given
port number.
�.�zPORT �,)�splitr[�joinr�)r&r'r<�hbytes�pbytes�bytesr�s r�sendportzFTP.sendport s`�����C�����t�S�y�/�/�4��S��>�>�2�������������'���|�|�C� � � rc�(�d}|jtjkrd}|jtjkrd}|dkrt d���dt|��|t|��dg}dd�|��z}|�|��S)zESend an EPRT command with the current host and the given port number.rr
�zunsupported address familyrzEPRT �|)rCr@�AF_INET�AF_INET6rr[r�r�)r&r'r<rC�fieldsr�s r�sendeprtzFTP.sendeprt*s���
���7�f�n�$�$��B��7�f�o�%�%��B�
��7�7��:�;�;�;��d�2�h�h��d�4�j�j�"�5�������(�(�(���|�|�C� � � rc��tjd|jd���}|���d}|j���d}|jtjkr|�||��}n|�||��}|jtur|�
|j��|S)z3Create a new socket and send a PORT command for it.)rrr
)rB�backlogr)r@�
create_serverrC�getsocknamer1r�r�r�r#r�
settimeout)r&r1r<r'r~s r�makeportzFTP.makeport7s����#�G�D�G�Q�G�G�G�����!�!�!�$���y�$�$�&�&�q�)���7�f�n�$�$��=�=��t�,�,�D�D��=�=��t�,�,�D��<�6�6�6��O�O�D�L�)�)�)��rc�L�|jtjkrOt|�d����\}}|jr|}n]|j���d}n=t|�d��|j�����\}}||fS)z<Internal: Does the PASV or EPSV handshake -> (address, port)�PASVr�EPSV) rCr@r��parse227r��trust_server_pasv_ipv4_addressr1�getpeername�parse229)r&�untrusted_hostr<r's r�makepasvzFTP.makepasvDs����7�f�n�$�$�#+�D�L�L��,@�,@�#A�#A� �N�D��2�
2�%����y�,�,�.�.�q�1���!�$�,�,�v�"6�"6�� �8M�8M�8O�8O�P�P�J�D�$��T�z�rc��d}|jr�|���\}}tj||f|j|j���} |�|�d|z��|�|��}|ddkr|���}|ddkrt|���n�#|� ���xYw|�
��5}|�|�d|z��|�|��}|ddkr|���}|ddkrt|���|���\}} |jtur|�
|j��ddd��n#1swxYwY|dd�dkrt|��}||fS) 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:zREST %srryrxrq�150)rQr�r@rAr#r"r�rFrr5r��acceptrr��parse150)
r&r��rest�sizer'r<�connr~r1�sockaddrs
r�ntransfercmdzFTP.ntransfercmdPs�������! 2������J�D�$��+�T�4�L�$�,�;?�;N�P�P�P�D�
��#��L�L��T�!1�2�2�2��|�|�C�(�(����7�c�>�>��<�<�>�>�D���7�c�>�>�%�d�+�+�+�"��
��
�
�������������
2�D��#��L�L��T�!1�2�2�2��|�|�C�(�(����7�c�>�>��<�<�>�>�D���7�c�>�>�%�d�+�+�+�!%�������h��<�'>�>�>��O�O�D�L�1�1�1�
2�
2�
2�
2�
2�
2�
2�
2�
2�
2�
2����
2�
2�
2�
2�����8�u����D�>�>�D��T�z�s �A*B0�0C�B*F�F�Fc�:�|�||��dS)z0Like ntransfercmd() but returns only the socket.r)r�)r&r�r�s r�transfercmdzFTP.transfercmd�s��� � ��d�+�+�A�.�.rc�2�|sd}|sd}|sd}|dkr |dvr|dz}|�d|z��}|ddkr|�d|z��}|ddkr|�d |z��}|dd
krt|���|S)zLogin, default anonymous.� anonymousr>rrsz
anonymous@zUSER rrzrW�ACCT ry�r�r)r&r(r)r*r~s rr%z FTP.login�s���� ��D�� ��F�� ��D��;���6�Y�#6�#6��l�*�F��|�|�G�d�N�+�+����7�c�>�>��<�<��&� 0�1�1�D���7�c�>�>��<�<��$��/�/�D���7�c�>�>��d�#�#�#��rrc�Z�|�d��|�||��5} |�|��}|sn||���$t�)t |t��r|���ddd��n#1swxYwY|���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 Ir
N)r�r��recv�
_SSLSocket�
isinstance�unwrapr�)r&r��callback� blocksizer�r��datas r�
retrbinaryzFTP.retrbinary�s���
���X����
�
�
�c�4�
(�
(� �D�
��y�y��+�+�����������
��%�*�T�:�*F�*F�%����
�
�
� � � � � � � � � � � ���� � � � ��}�}���s�AB�B�Bc��|�t}|�d��}|�|��5}|�d|j���5} |�|jdz��}t|��|jkrtd|jz���|j dkrtdt|����|snB|d d�tkr|dd �}n|d
d�dkr
|dd
�}||����t�)t|t��r|���ddd��n#1swxYwYddd��n#1swxYwY|���S)ahRetrieve 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;rr
rjr�z*retr*rkrlr`)�
print_liner�r�rDr rmrnrYrrIrJr[rar�r�r�r�)r&r�r�r~r��fpres r� retrlinesz
FTP.retrlines�s�����!�H��|�|�H�%�%��
�
�
�c�
"�
"� �d����s�T�]��;�;� �?A�
��{�{�4�<�!�#3�4�4���t�9�9�t�|�+�+�� 8�4�<� G�H�H�H��>�A�%�%��(�D��J�J�/�/�/��������9��$�$�����9�D�D��"�#�#�Y�$�&�&�����9�D�������
��%�*�T�:�*F�*F�%����
�
�
�# � � � � � � � � � � ���� � � � � � � � � � � � � � � ���� � � � �$�}�}���s6�E�C+E�<E�E �E�E �E�E#�&E#c��|�d��|�||��5} |�|��}|sn#|�|��|r||���;t�)t|t��r|���ddd��n#1swxYwY|���S)a9Store 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�r
N)r�r��readrbr�r�r�r�)r&r�r�r�r�r�r��bufs r�
storbinaryzFTP.storbinary�s���
���X����
�
�
�c�4�
(�
(�
�D�
"��g�g�i�(�(��������S�!�!�!��"��H�S�M�M�M�
"��%�*�T�:�*F�*F�%����
�
�
�
�
�
�
�
�
�
�
�
�
�
����
�
�
�
��}�}���s�A-B%�%B)�,B)c�`�|�d��|�|��5} |�|jdz��}t |��|jkrtd|jz���|snY|dd�tkr#|dtvr
|dd�}|tz}|�|��|r||����t�)t|t��r|�
��ddd��n#1swxYwY|���S)ahStore 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
rjrkNrl)r�r�rmrnrYrr�rbr�r�r�r�)r&r�r�r�r�r�s r� storlinesz
FTP.storlines�sd��
���X����
�
�
�c�
"�
"� �d�
"��k�k�$�,��"2�3�3���s�8�8�d�l�*�*�� 8�4�<� G�H�H�H�����r�s�s�8�v�%�%��2�w�&�(�(��C�R�C��#���,�C����S�!�!�!��"��H�S�M�M�M�
"��%�*�T�:�*F�*F�%����
�
�
� � � � � � � � � � � ���� � � � � �}�}���s�CD�D�Dc�6�d|z}|�|��S)zSend new account name.r��r�)r&�passwordr�s rr*zFTP.accts���� ���|�|�C� � � rc�^�d}|D]
}|d|zz}�g}|�||j��|S)zBReturn a list of files in a given directory (default the current).�NLST� )r��append)r&r6r��arg�filess r�nlstzFTP.nlst#sF����� $� $�C���s��#�C�C������s�E�L�)�)�)��rc���d}d}|dd�r8t|d��td��kr|dd�|d}}|D]}|r|d|zz}�
|�||��dS)aList 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.)�LISTNrlrr�)�typer�)r&r6r��funcr�s r�dirzFTP.dir,s�����������9� -��d�2�h���4��8�8�3�3��c�r�c��D��H�$�D�� (� (�C��
(��S�3�Y�'������s�D�!�!�!�!�!rc#��K�|r.|�dd�|��zdz��|rd|z}nd}g}|�||j��|D]�}|�t
���d��\}}}i} |dd��d��D]2}
|
�d��\}}}|| |���<�3|| fV���dS) 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 �;zMLSD %s�MLSDr�Nrl�=) r�r�r�r�rZra� partitionr��lower)
r&�path�factsr��linesre�facts_found�_�name�entry�fact�key�values
r�mlsdzFTP.mlsd;s����� ?��L�L��������7�#�=�>�>�>�� ��d�"�C�C��C������s�E�L�)�)�)�� � �D�#'�;�;�t�#4�#4�#>�#>�s�#C�#C� �K��D��E�#�C�R�C�(�.�.�s�3�3�
+�
+�� $���s� 3� 3�
��Q��%*��c�i�i�k�k�"�"���-�����
� rc��|�d|z��}|ddkrt|���|�d|z��S)zRename a file.zRNFR rrzzRNTO )r�rr�)r&�fromname�tonamer~s r�renamez
FTP.renameWsJ���|�|�G�h�.�/�/����7�c�>�>��d�#�#�#��|�|�G�f�,�-�-�-rc�l�|�d|z��}|dd�dvr|St|���)zDelete a file.zDELE Nrq>�200�250r�)r&�filenamer~s r�deletez
FTP.delete^s>���|�|�G�h�.�/�/������8�~�%�%��K��d�#�#�#rc���|dkrG |�d��S#t$r$}|jddd�dkr�Yd}~nd}~wwxYw|dkrd}d |z}|�|��S)
zChange to a directory.z..�CDUPrNrq�500rr�zCWD )r�rr6)r&�dirname�msgr�s r�cwdzFTP.cwdfs����d�?�?�
��|�|�F�+�+�+���
�
�
��8�A�;�r��r�?�e�+�+��,�+�+�+�+�����
������]�]��G��w����|�|�C� � � s��
A�A�Ac��|�d|z��}|dd�dkr+|dd����}t|��SdS)zRetrieve the size of a file.zSIZE Nrq�213)r��strip�int)r&r�r~r\s rr�zFTP.sizessW���|�|�G�h�.�/�/������8�u����Q�R�R���� � �A��q�6�6�M��rc�~�|�d|z��}|�d��sdSt|��S)z+Make a directory, return its full pathname.zMKD �257r�r��
startswith�parse257)r&rr~s r�mkdzFTP.mkd{s?���|�|�F�W�,�-�-�����u�%�%� ��2���~�~�rc�2�|�d|z��S)zRemove a directory.zRMD r�)r&rs r�rmdzFTP.rmd�s���|�|�F�W�,�-�-�-rc�x�|�d��}|�d��sdSt|��S)z!Return current working directory.�PWDrrrr�s r�pwdzFTP.pwd�s:���|�|�E�"�"�����u�%�%� ��2���~�~�rc�X�|�d��}|���|S)zQuit, and close the connection.�QUIT)r�r5r�s rr2zFTP.quit�s%���|�|�F�#�#���
�
�����rc��� |j}d|_|�|���|j}d|_|�|���dSdS#|j}d|_|�|���wwxYw)z8Close the connection without assuming anything about it.N)rEr5r1)r&rEr1s rr5z FTP.close�s��� ��9�D��D�I����
�
�����9�D��D�I����
�
������ ����9�D��D�I����
�
����� ���s�$A�'A5)rrr8Nr-)rrr)rN)rNN):rrr�__doc__rIr'�FTP_PORTr<�MAXLINErnr1rErGrQr�rr+r/r7r$rLrO�debugrSrKrfrhrorvrFr�r�r�r�r�r�r�r�r�r�r%r�r�r�r�r*r�r�r�r�r�rr�rrrr2r5rrrrrJs��������.�I�
�D��D��G��D��D��G��M�%*�"��R���0��/�!�/�/�/�/�/� ���!�!�!�����4������
�E�!�!�!����6�6�6�������$
�
�
� � � ����������
���
!�!�!�!�!�!����
�
�
�5�5�5�5�n/�/�/�/�����4����4!�!�!�!�F����:����>!�!�!�
���
"�
"�
"��"� � � � �8.�.�.�$�$�$�!�!�!�������.�.�.�����������rrc ���eZdZdZejZdddddddedf dd��fd�Zd�fd� Z d �Z
d
�Zd�Zd�Z
d�fd
� Zd�Z�xZS)�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.'
>>>
rNrrc
�`��|�|�td���|�|�td���|�|� ddl}|�dtd��||_||_|�t
j|j||���}||_ d|_
t���|||||| |
���dS) Nz4context and keyfile arguments are mutually exclusivez5context and certfile arguments are mutually exclusiverzAkeyfile and certfile are deprecated, use a custom context insteadr�)�certfile�keyfileFr)
r=�warnings�warn�DeprecationWarningr r�ssl�_create_stdlib_context�ssl_version�context�_prot_p�superr+)
r&r'r(r)r*r rr'r#r"r r!� __class__s
�rr+zFTP_TLS.__init__�s����"�w�':� �"-�.�.�.��"�x�';� �"-�.�.�.��"�h�&:������
�
�7�8J�A�O�O�O�"�D�L�$�D�M����4�T�5E�>F�=D�F�F�F��#�D�L� �D�L��G�G���T�4���$�n�x�
�
I�
I�
I�
I�
IrTc���|r3t|jtj��s|���t���|||��Sr-)r�r1r$� SSLSocket�authr)r%)r&r(r)r*�securer*s �rr%z
FTP_TLS.login�sG����
�j���C�M�B�B�
�� � �����7�7�=�=��v�t�4�4�4rc��t|jtj��rt d���|jtjkr|�d��}n|�d��}|j� |j|j
���|_|j�d|j���|_
|S)z2Set up secure control connection by using TLS/SSL.zAlready using TLSzAUTH TLSzAUTH SSL��server_hostnamer;)�moder )r�r1r$r,r=r&�PROTOCOL_TLSr�r'�wrap_socketr'rDr rEr�s rr-zFTP_TLS.auth�s����$�)�S�]�3�3�
6� �!4�5�5�5���3�#3�3�3��|�|�J�/�/����|�|�J�/�/����0�0���D�I�0�V�V�D�I�� �*�*��d�m�*�L�L�D�I��Krc���t|jtj��st d���|�d��}|j���|_|S)z/Switch back to a clear-text control connection.z
not using TLS�CCC)r�r1r$r,r=r�r�r�s r�ccczFTP_TLS.ccc�sQ���d�i���7�7�
2� ��1�1�1��<�<��&�&�D�� �(�(�*�*�D�I��Krc�h�|�d��|�d��}d|_|S)zSet up secure data connection.zPBSZ 0zPROT PT�r�r(r�s r�prot_pzFTP_TLS.prot_ps3��
�L�L��"�"�"��<�<��)�)�D��D�L��Krc�>�|�d��}d|_|S)z"Set up clear text data connection.zPROT CFr9r�s r�prot_czFTP_TLS.prot_cs���<�<��)�)�D� �D�L��Krc���t���||��\}}|jr!|j�||j���}||fS)Nr0)r)r�r(r'r4r')r&r�r�r�r�r*s �rr�zFTP_TLS.ntransfercmdsZ������-�-�c�4�8�8�J�D�$��|�
K��|�/�/��@D� �0�K�K����:�rc��dtz}|j�|��|���}|dd�dvrt |���|S)Nr�rqr�)r�r1rbrvrr�s rr�z
FTP_TLS.abortsZ���V�#�D��I���d�#�#�#��$�$�&�&�D��B�Q�B�x�4�4�4�!�$�'�'�'��Kr)rrrTr-)rrrrr$�PROTOCOL_TLS_CLIENTr&rr+r%r-r7r:r<r�r��
__classcell__)r*s@rrr�s������� � �@�-�� "��B�R�!�D�$�4�T� I�&� I� I� I� I� I� I� I�2 5� 5� 5� 5� 5� 5�
�
�
� � � � � � � � � � � � � � � � � � � � � � rrc��|dd�dkrt|���t�'ddl}|�d|j|jz��at�|��}|sdSt|�d����S)z�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.
Nrqr�rz150 .* \((\d+) bytes\)r
) r�_150_re�re�compile�
IGNORECASE�ASCII�matchr �group)r~rC�ms rr�r�.s���
�B�Q�B�x�5����$������� � � ��*�*�%�r�}�r�x�'?�A�A���
�
�d���A����t��q�w�w�q�z�z�?�?�rc��|dd�dkrt|���t�ddl}|�d|j��at�|��}|st
|���|���}d�|dd���}t|d��dzt|d ��z}||fS)
z�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.Nrq�227rz#(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)r�rr�rU)
r�_227_rerCrDrF�searchr�groupsr�r )r~rCrI�numbersr'r<s rr�r�Bs����B�Q�B�x�5����$������� � � ��*�*�C�R�X�N�N�����t���A�� ��$�����h�h�j�j�G��8�8�G�B�Q�B�K� � �D����
�O�O�q� �C���
�O�O�3�D���:�rc��|dd�dkrt|���|�d��}|dkrt|���|�d|dz��}|dkrt|���||dz||dz
krt|���||dz|��||dz��}t |��dkrt|���|d}t|d��}||fS) z�Parse the '229' response for an EPSV request.
Raises error_proto if it does not contain '(|||port|)'
Return ('host.addr.as.numbers', port#) tuple.Nrq�229�(r�)r
rU)r�findrr�rYr )r~�peer�left�right�partsr'r<s rr�r�Us���B�Q�B�x�5����$�����9�9�S�>�>�D��a�x�x�{�4�(�(�(��I�I�c�4�!�8�$�$�E��q�y�y��$�����D�1�H�~��e�a�i��(�(��$��������%�� �&�&�t�D��F�|�4�4�E�
�5�z�z�Q����$������7�D��u�Q�x�=�=�D���:�rc��|dd�dkrt|���|dd�dkrdSd}d}t|��}||kr6||}|dz}|dkr||ks||dkrn|dz}||z}||k�6|S) z�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.NrqrrUz "rr
�")rrY)r~rr]�nrs rrrjs����B�Q�B�x�5����$�����A�a�C�y�D����r��G� �A��D� � �A�
�a�%�%���G��
�a�C����8�8��A�v�v��a��C�����!��A��A�+���a�%�%��Nrc�$�t|��dS)z+Default retrlines callback to print a line.N)rJ)res rr�r��s�� �$�K�K�K�K�Krr�Ic���|s|}d|z}|�|��|�|��t|�d����\}}|�||��|�d|z��}|dd�dvrt�|�d|z��}|dd�dvrt�|���|���dS)z+Copy file from one FTP-instance to another.zTYPE r�zSTOR Nrq>�125r��RETR )r�r�r�r�rr�) �source�
sourcename�target�
targetnamer��
sourcehost�
sourceport�treply�sreplys r�ftpcprj�s���� ��
��T�>�D�
�N�N�4����
�N�N�4����%�f�n�n�V�&<�&<�=�=��J�
�
�O�O�J�
�+�+�+��^�^�G�j�0�
1�
1�F�
�b�q�b�z��'�'���
�^�^�G�j�0�
1�
1�F�
�b�q�b�z��'�'���
�O�O����
�O�O�����rc���ttj��dkr-ttj��tjd��ddl}d}d}tjddkr(|dz}tjd=tjddk�(tjddd�dkr'tjddd�}tjd=tjd}t|��}|� |��dx}x}} |�|��} |�
|��\}}}n_#ttf$rtdtj
� ��Yn1wxYw#t$r |�td
tj
� ��YnwxYw|�|||��tjdd�D�]} | dd�dkr|�| dd���n�| dd�dkr2d}
| dd�r|
d
z| dd�z}
|�|
��}ns| dkr|�|j��nQ|�d| ztjjjd��tjj���tj�����|���dS)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�rNr
z-dz-rrz$No account -- using anonymous login.)rEz5Could not open account file -- using anonymous login.z-l�CWDr�z-prai)rYr>�argvrJ�testr�exit�netrcrrO�authenticators�KeyError� TypeError�stderrr3r%r�r�rSrQr��stdout�buffer�write�flushr2)rprI�rcfiler'�ftp�useridr)r*�netrcobjrEr�r~s rrnrn�s����3�8�}�}�q���
�d�l�����������L�L�L��I�
�F�
�(�1�+��
�
��a�K� ��H�Q�K��(�1�+��
�
��x��{�2�A�2��$�����!��Q�R�R����H�Q�K��8�A�;�D�
�d�)�)�C����y�!�!�!���F��V�d�K��;�;�v�&�&�� K�#+�#:�#:�4�#@�#@� �F�D�&�&���)�$� K� K� K��8�s�z�J�J�J�J�J�J� K������#�#�#����I��z�
#�
#�
#�
#���#�����I�I�f�f�d�#�#�#�������
�
������8�t����G�G�D����H�����
�"�1�"�X��
�
��C��A�B�B�x�3�s�S�y�4����8�3���;�;�s�#�#�D�D�
�T�\�\��L�L�S�.�.�/�/�/�/��N�N�7�T�>��:�,�2�D�
:�
:�
:��J��#�#�%�%�%��
��������H�H�J�J�J�J�Js$�E5�)E�,E2�1E2�5'F�F�__main__)rr^)%rr>r@r�__all__r�rr� Exceptionrrrrrr3r4r rar�rr$r,r�rr��SSLError�ImportErrorrBr�rMr�r�rr�rjrnrrrr�<module>r�sc����L�
�
�
�
�
�
�
�*�*�*�*�*�*���������
�������I���������%���������������������������%����
�W�h�
'�
��� ��X �X �X �X �X �X �X �X �tE:��J�J�J���J�{�{�{�{�{�#�{�{�{�z�N�N�9������(�C�L�9�J�J��G�����J�J�J�����L�����$�����&���*���,���
����,=�=�=�@�z����D�F�F�F�F�F��s�8B2�2B<�;B< |