Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3....
File: ftplib.py
"""An FTP client class and some helper functions.
[0] Fix | Delete
[1] Fix | Delete
Based on RFC 959: File Transfer Protocol (FTP), by J. Postel and J. Reynolds
[2] Fix | Delete
[3] Fix | Delete
Example:
[4] Fix | Delete
[5] Fix | Delete
>>> from ftplib import FTP
[6] Fix | Delete
>>> ftp = FTP('ftp.python.org') # connect to host, default port
[7] Fix | Delete
>>> ftp.login() # default, i.e.: user anonymous, passwd anonymous@
[8] Fix | Delete
'230 Guest login ok, access restrictions apply.'
[9] Fix | Delete
>>> ftp.retrlines('LIST') # list directory contents
[10] Fix | Delete
total 9
[11] Fix | Delete
drwxr-xr-x 8 root wheel 1024 Jan 3 1994 .
[12] Fix | Delete
drwxr-xr-x 8 root wheel 1024 Jan 3 1994 ..
[13] Fix | Delete
drwxr-xr-x 2 root wheel 1024 Jan 3 1994 bin
[14] Fix | Delete
drwxr-xr-x 2 root wheel 1024 Jan 3 1994 etc
[15] Fix | Delete
d-wxrwxr-x 2 ftp wheel 1024 Sep 5 13:43 incoming
[16] Fix | Delete
drwxr-xr-x 2 root wheel 1024 Nov 17 1993 lib
[17] Fix | Delete
drwxr-xr-x 6 1094 wheel 1024 Sep 13 19:07 pub
[18] Fix | Delete
drwxr-xr-x 3 root wheel 1024 Jan 3 1994 usr
[19] Fix | Delete
-rw-r--r-- 1 root root 312 Aug 1 1994 welcome.msg
[20] Fix | Delete
'226 Transfer complete.'
[21] Fix | Delete
>>> ftp.quit()
[22] Fix | Delete
'221 Goodbye.'
[23] Fix | Delete
>>>
[24] Fix | Delete
[25] Fix | Delete
A nice test that reveals some of the network dialogue would be:
[26] Fix | Delete
python ftplib.py -d localhost -l -p -l
[27] Fix | Delete
"""
[28] Fix | Delete
[29] Fix | Delete
#
[30] Fix | Delete
# Changes and improvements suggested by Steve Majewski.
[31] Fix | Delete
# Modified by Jack to work on the mac.
[32] Fix | Delete
# Modified by Siebren to support docstrings and PASV.
[33] Fix | Delete
# Modified by Phil Schwartz to add storbinary and storlines callbacks.
[34] Fix | Delete
# Modified by Giampaolo Rodola' to add TLS support.
[35] Fix | Delete
#
[36] Fix | Delete
[37] Fix | Delete
import sys
[38] Fix | Delete
import socket
[39] Fix | Delete
from socket import _GLOBAL_DEFAULT_TIMEOUT
[40] Fix | Delete
[41] Fix | Delete
__all__ = ["FTP", "error_reply", "error_temp", "error_perm", "error_proto",
[42] Fix | Delete
"all_errors"]
[43] Fix | Delete
[44] Fix | Delete
# Magic number from <socket.h>
[45] Fix | Delete
MSG_OOB = 0x1 # Process data out of band
[46] Fix | Delete
[47] Fix | Delete
[48] Fix | Delete
# The standard FTP server control port
[49] Fix | Delete
FTP_PORT = 21
[50] Fix | Delete
# The sizehint parameter passed to readline() calls
[51] Fix | Delete
MAXLINE = 8192
[52] Fix | Delete
[53] Fix | Delete
[54] Fix | Delete
# Exception raised when an error or invalid response is received
[55] Fix | Delete
class Error(Exception): pass
[56] Fix | Delete
class error_reply(Error): pass # unexpected [123]xx reply
[57] Fix | Delete
class error_temp(Error): pass # 4xx errors
[58] Fix | Delete
class error_perm(Error): pass # 5xx errors
[59] Fix | Delete
class error_proto(Error): pass # response does not begin with [1-5]
[60] Fix | Delete
[61] Fix | Delete
[62] Fix | Delete
# All exceptions (hopefully) that may be raised here and that aren't
[63] Fix | Delete
# (always) programming errors on our side
[64] Fix | Delete
all_errors = (Error, OSError, EOFError)
[65] Fix | Delete
[66] Fix | Delete
[67] Fix | Delete
# Line terminators (we always output CRLF, but accept any of CRLF, CR, LF)
[68] Fix | Delete
CRLF = '\r\n'
[69] Fix | Delete
B_CRLF = b'\r\n'
[70] Fix | Delete
[71] Fix | Delete
# The class itself
[72] Fix | Delete
class FTP:
[73] Fix | Delete
[74] Fix | Delete
'''An FTP client class.
[75] Fix | Delete
[76] Fix | Delete
To create a connection, call the class using these arguments:
[77] Fix | Delete
host, user, passwd, acct, timeout
[78] Fix | Delete
[79] Fix | Delete
The first four arguments are all strings, and have default value ''.
[80] Fix | Delete
timeout must be numeric and defaults to None if not passed,
[81] Fix | Delete
meaning that no timeout will be set on any ftp socket(s)
[82] Fix | Delete
If a timeout is passed, then this is now the default timeout for all ftp
[83] Fix | Delete
socket operations for this instance.
[84] Fix | Delete
[85] Fix | Delete
Then use self.connect() with optional host and port argument.
[86] Fix | Delete
[87] Fix | Delete
To download a file, use ftp.retrlines('RETR ' + filename),
[88] Fix | Delete
or ftp.retrbinary() with slightly different arguments.
[89] Fix | Delete
To upload a file, use ftp.storlines() or ftp.storbinary(),
[90] Fix | Delete
which have an open file as argument (see their definitions
[91] Fix | Delete
below for details).
[92] Fix | Delete
The download/upload functions first issue appropriate TYPE
[93] Fix | Delete
and PORT or PASV commands.
[94] Fix | Delete
'''
[95] Fix | Delete
[96] Fix | Delete
debugging = 0
[97] Fix | Delete
host = ''
[98] Fix | Delete
port = FTP_PORT
[99] Fix | Delete
maxline = MAXLINE
[100] Fix | Delete
sock = None
[101] Fix | Delete
file = None
[102] Fix | Delete
welcome = None
[103] Fix | Delete
passiveserver = 1
[104] Fix | Delete
encoding = "latin-1"
[105] Fix | Delete
# Disables https://bugs.python.org/issue43285 security if set to True.
[106] Fix | Delete
trust_server_pasv_ipv4_address = False
[107] Fix | Delete
[108] Fix | Delete
# Initialization method (called by class instantiation).
[109] Fix | Delete
# Initialize host to localhost, port to standard ftp port
[110] Fix | Delete
# Optional arguments are host (for connect()),
[111] Fix | Delete
# and user, passwd, acct (for login())
[112] Fix | Delete
def __init__(self, host='', user='', passwd='', acct='',
[113] Fix | Delete
timeout=_GLOBAL_DEFAULT_TIMEOUT, source_address=None):
[114] Fix | Delete
self.source_address = source_address
[115] Fix | Delete
self.timeout = timeout
[116] Fix | Delete
if host:
[117] Fix | Delete
self.connect(host)
[118] Fix | Delete
if user:
[119] Fix | Delete
self.login(user, passwd, acct)
[120] Fix | Delete
[121] Fix | Delete
def __enter__(self):
[122] Fix | Delete
return self
[123] Fix | Delete
[124] Fix | Delete
# Context management protocol: try to quit() if active
[125] Fix | Delete
def __exit__(self, *args):
[126] Fix | Delete
if self.sock is not None:
[127] Fix | Delete
try:
[128] Fix | Delete
self.quit()
[129] Fix | Delete
except (OSError, EOFError):
[130] Fix | Delete
pass
[131] Fix | Delete
finally:
[132] Fix | Delete
if self.sock is not None:
[133] Fix | Delete
self.close()
[134] Fix | Delete
[135] Fix | Delete
def connect(self, host='', port=0, timeout=-999, source_address=None):
[136] Fix | Delete
'''Connect to host. Arguments are:
[137] Fix | Delete
- host: hostname to connect to (string, default previous host)
[138] Fix | Delete
- port: port to connect to (integer, default previous port)
[139] Fix | Delete
- timeout: the timeout to set against the ftp socket(s)
[140] Fix | Delete
- source_address: a 2-tuple (host, port) for the socket to bind
[141] Fix | Delete
to as its source address before connecting.
[142] Fix | Delete
'''
[143] Fix | Delete
if host != '':
[144] Fix | Delete
self.host = host
[145] Fix | Delete
if port > 0:
[146] Fix | Delete
self.port = port
[147] Fix | Delete
if timeout != -999:
[148] Fix | Delete
self.timeout = timeout
[149] Fix | Delete
if source_address is not None:
[150] Fix | Delete
self.source_address = source_address
[151] Fix | Delete
self.sock = socket.create_connection((self.host, self.port), self.timeout,
[152] Fix | Delete
source_address=self.source_address)
[153] Fix | Delete
self.af = self.sock.family
[154] Fix | Delete
self.file = self.sock.makefile('r', encoding=self.encoding)
[155] Fix | Delete
self.welcome = self.getresp()
[156] Fix | Delete
return self.welcome
[157] Fix | Delete
[158] Fix | Delete
def getwelcome(self):
[159] Fix | Delete
'''Get the welcome message from the server.
[160] Fix | Delete
(this is read and squirreled away by connect())'''
[161] Fix | Delete
if self.debugging:
[162] Fix | Delete
print('*welcome*', self.sanitize(self.welcome))
[163] Fix | Delete
return self.welcome
[164] Fix | Delete
[165] Fix | Delete
def set_debuglevel(self, level):
[166] Fix | Delete
'''Set the debugging level.
[167] Fix | Delete
The required argument level means:
[168] Fix | Delete
0: no debugging output (default)
[169] Fix | Delete
1: print commands and responses but not body text etc.
[170] Fix | Delete
2: also print raw lines read and sent before stripping CR/LF'''
[171] Fix | Delete
self.debugging = level
[172] Fix | Delete
debug = set_debuglevel
[173] Fix | Delete
[174] Fix | Delete
def set_pasv(self, val):
[175] Fix | Delete
'''Use passive or active mode for data transfers.
[176] Fix | Delete
With a false argument, use the normal PORT mode,
[177] Fix | Delete
With a true argument, use the PASV command.'''
[178] Fix | Delete
self.passiveserver = val
[179] Fix | Delete
[180] Fix | Delete
# Internal: "sanitize" a string for printing
[181] Fix | Delete
def sanitize(self, s):
[182] Fix | Delete
if s[:5] in {'pass ', 'PASS '}:
[183] Fix | Delete
i = len(s.rstrip('\r\n'))
[184] Fix | Delete
s = s[:5] + '*'*(i-5) + s[i:]
[185] Fix | Delete
return repr(s)
[186] Fix | Delete
[187] Fix | Delete
# Internal: send one line to the server, appending CRLF
[188] Fix | Delete
def putline(self, line):
[189] Fix | Delete
if '\r' in line or '\n' in line:
[190] Fix | Delete
raise ValueError('an illegal newline character should not be contained')
[191] Fix | Delete
line = line + CRLF
[192] Fix | Delete
if self.debugging > 1:
[193] Fix | Delete
print('*put*', self.sanitize(line))
[194] Fix | Delete
self.sock.sendall(line.encode(self.encoding))
[195] Fix | Delete
[196] Fix | Delete
# Internal: send one command to the server (through putline())
[197] Fix | Delete
def putcmd(self, line):
[198] Fix | Delete
if self.debugging: print('*cmd*', self.sanitize(line))
[199] Fix | Delete
self.putline(line)
[200] Fix | Delete
[201] Fix | Delete
# Internal: return one line from the server, stripping CRLF.
[202] Fix | Delete
# Raise EOFError if the connection is closed
[203] Fix | Delete
def getline(self):
[204] Fix | Delete
line = self.file.readline(self.maxline + 1)
[205] Fix | Delete
if len(line) > self.maxline:
[206] Fix | Delete
raise Error("got more than %d bytes" % self.maxline)
[207] Fix | Delete
if self.debugging > 1:
[208] Fix | Delete
print('*get*', self.sanitize(line))
[209] Fix | Delete
if not line:
[210] Fix | Delete
raise EOFError
[211] Fix | Delete
if line[-2:] == CRLF:
[212] Fix | Delete
line = line[:-2]
[213] Fix | Delete
elif line[-1:] in CRLF:
[214] Fix | Delete
line = line[:-1]
[215] Fix | Delete
return line
[216] Fix | Delete
[217] Fix | Delete
# Internal: get a response from the server, which may possibly
[218] Fix | Delete
# consist of multiple lines. Return a single string with no
[219] Fix | Delete
# trailing CRLF. If the response consists of multiple lines,
[220] Fix | Delete
# these are separated by '\n' characters in the string
[221] Fix | Delete
def getmultiline(self):
[222] Fix | Delete
line = self.getline()
[223] Fix | Delete
if line[3:4] == '-':
[224] Fix | Delete
code = line[:3]
[225] Fix | Delete
while 1:
[226] Fix | Delete
nextline = self.getline()
[227] Fix | Delete
line = line + ('\n' + nextline)
[228] Fix | Delete
if nextline[:3] == code and \
[229] Fix | Delete
nextline[3:4] != '-':
[230] Fix | Delete
break
[231] Fix | Delete
return line
[232] Fix | Delete
[233] Fix | Delete
# Internal: get a response from the server.
[234] Fix | Delete
# Raise various errors if the response indicates an error
[235] Fix | Delete
def getresp(self):
[236] Fix | Delete
resp = self.getmultiline()
[237] Fix | Delete
if self.debugging:
[238] Fix | Delete
print('*resp*', self.sanitize(resp))
[239] Fix | Delete
self.lastresp = resp[:3]
[240] Fix | Delete
c = resp[:1]
[241] Fix | Delete
if c in {'1', '2', '3'}:
[242] Fix | Delete
return resp
[243] Fix | Delete
if c == '4':
[244] Fix | Delete
raise error_temp(resp)
[245] Fix | Delete
if c == '5':
[246] Fix | Delete
raise error_perm(resp)
[247] Fix | Delete
raise error_proto(resp)
[248] Fix | Delete
[249] Fix | Delete
def voidresp(self):
[250] Fix | Delete
"""Expect a response beginning with '2'."""
[251] Fix | Delete
resp = self.getresp()
[252] Fix | Delete
if resp[:1] != '2':
[253] Fix | Delete
raise error_reply(resp)
[254] Fix | Delete
return resp
[255] Fix | Delete
[256] Fix | Delete
def abort(self):
[257] Fix | Delete
'''Abort a file transfer. Uses out-of-band data.
[258] Fix | Delete
This does not follow the procedure from the RFC to send Telnet
[259] Fix | Delete
IP and Synch; that doesn't seem to work with the servers I've
[260] Fix | Delete
tried. Instead, just send the ABOR command as OOB data.'''
[261] Fix | Delete
line = b'ABOR' + B_CRLF
[262] Fix | Delete
if self.debugging > 1:
[263] Fix | Delete
print('*put urgent*', self.sanitize(line))
[264] Fix | Delete
self.sock.sendall(line, MSG_OOB)
[265] Fix | Delete
resp = self.getmultiline()
[266] Fix | Delete
if resp[:3] not in {'426', '225', '226'}:
[267] Fix | Delete
raise error_proto(resp)
[268] Fix | Delete
return resp
[269] Fix | Delete
[270] Fix | Delete
def sendcmd(self, cmd):
[271] Fix | Delete
'''Send a command and return the response.'''
[272] Fix | Delete
self.putcmd(cmd)
[273] Fix | Delete
return self.getresp()
[274] Fix | Delete
[275] Fix | Delete
def voidcmd(self, cmd):
[276] Fix | Delete
"""Send a command and expect a response beginning with '2'."""
[277] Fix | Delete
self.putcmd(cmd)
[278] Fix | Delete
return self.voidresp()
[279] Fix | Delete
[280] Fix | Delete
def sendport(self, host, port):
[281] Fix | Delete
'''Send a PORT command with the current host and the given
[282] Fix | Delete
port number.
[283] Fix | Delete
'''
[284] Fix | Delete
hbytes = host.split('.')
[285] Fix | Delete
pbytes = [repr(port//256), repr(port%256)]
[286] Fix | Delete
bytes = hbytes + pbytes
[287] Fix | Delete
cmd = 'PORT ' + ','.join(bytes)
[288] Fix | Delete
return self.voidcmd(cmd)
[289] Fix | Delete
[290] Fix | Delete
def sendeprt(self, host, port):
[291] Fix | Delete
'''Send an EPRT command with the current host and the given port number.'''
[292] Fix | Delete
af = 0
[293] Fix | Delete
if self.af == socket.AF_INET:
[294] Fix | Delete
af = 1
[295] Fix | Delete
if self.af == socket.AF_INET6:
[296] Fix | Delete
af = 2
[297] Fix | Delete
if af == 0:
[298] Fix | Delete
raise error_proto('unsupported address family')
[299] Fix | Delete
fields = ['', repr(af), host, repr(port), '']
[300] Fix | Delete
cmd = 'EPRT ' + '|'.join(fields)
[301] Fix | Delete
return self.voidcmd(cmd)
[302] Fix | Delete
[303] Fix | Delete
def makeport(self):
[304] Fix | Delete
'''Create a new socket and send a PORT command for it.'''
[305] Fix | Delete
err = None
[306] Fix | Delete
sock = None
[307] Fix | Delete
for res in socket.getaddrinfo(None, 0, self.af, socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
[308] Fix | Delete
af, socktype, proto, canonname, sa = res
[309] Fix | Delete
try:
[310] Fix | Delete
sock = socket.socket(af, socktype, proto)
[311] Fix | Delete
sock.bind(sa)
[312] Fix | Delete
except OSError as _:
[313] Fix | Delete
err = _
[314] Fix | Delete
if sock:
[315] Fix | Delete
sock.close()
[316] Fix | Delete
sock = None
[317] Fix | Delete
continue
[318] Fix | Delete
break
[319] Fix | Delete
if sock is None:
[320] Fix | Delete
if err is not None:
[321] Fix | Delete
raise err
[322] Fix | Delete
else:
[323] Fix | Delete
raise OSError("getaddrinfo returns an empty list")
[324] Fix | Delete
sock.listen(1)
[325] Fix | Delete
port = sock.getsockname()[1] # Get proper port
[326] Fix | Delete
host = self.sock.getsockname()[0] # Get proper host
[327] Fix | Delete
if self.af == socket.AF_INET:
[328] Fix | Delete
resp = self.sendport(host, port)
[329] Fix | Delete
else:
[330] Fix | Delete
resp = self.sendeprt(host, port)
[331] Fix | Delete
if self.timeout is not _GLOBAL_DEFAULT_TIMEOUT:
[332] Fix | Delete
sock.settimeout(self.timeout)
[333] Fix | Delete
return sock
[334] Fix | Delete
[335] Fix | Delete
def makepasv(self):
[336] Fix | Delete
"""Internal: Does the PASV or EPSV handshake -> (address, port)"""
[337] Fix | Delete
if self.af == socket.AF_INET:
[338] Fix | Delete
untrusted_host, port = parse227(self.sendcmd('PASV'))
[339] Fix | Delete
if self.trust_server_pasv_ipv4_address:
[340] Fix | Delete
host = untrusted_host
[341] Fix | Delete
else:
[342] Fix | Delete
host = self.sock.getpeername()[0]
[343] Fix | Delete
else:
[344] Fix | Delete
host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername())
[345] Fix | Delete
return host, port
[346] Fix | Delete
[347] Fix | Delete
def ntransfercmd(self, cmd, rest=None):
[348] Fix | Delete
"""Initiate a transfer over the data connection.
[349] Fix | Delete
[350] Fix | Delete
If the transfer is active, send a port command and the
[351] Fix | Delete
transfer command, and accept the connection. If the server is
[352] Fix | Delete
passive, send a pasv command, connect to it, and start the
[353] Fix | Delete
transfer command. Either way, return the socket for the
[354] Fix | Delete
connection and the expected size of the transfer. The
[355] Fix | Delete
expected size may be None if it could not be determined.
[356] Fix | Delete
[357] Fix | Delete
Optional `rest' argument can be a string that is sent as the
[358] Fix | Delete
argument to a REST command. This is essentially a server
[359] Fix | Delete
marker used to tell the server to skip over any data up to the
[360] Fix | Delete
given marker.
[361] Fix | Delete
"""
[362] Fix | Delete
size = None
[363] Fix | Delete
if self.passiveserver:
[364] Fix | Delete
host, port = self.makepasv()
[365] Fix | Delete
conn = socket.create_connection((host, port), self.timeout,
[366] Fix | Delete
source_address=self.source_address)
[367] Fix | Delete
try:
[368] Fix | Delete
if rest is not None:
[369] Fix | Delete
self.sendcmd("REST %s" % rest)
[370] Fix | Delete
resp = self.sendcmd(cmd)
[371] Fix | Delete
# Some servers apparently send a 200 reply to
[372] Fix | Delete
# a LIST or STOR command, before the 150 reply
[373] Fix | Delete
# (and way before the 226 reply). This seems to
[374] Fix | Delete
# be in violation of the protocol (which only allows
[375] Fix | Delete
# 1xx or error messages for LIST), so we just discard
[376] Fix | Delete
# this response.
[377] Fix | Delete
if resp[0] == '2':
[378] Fix | Delete
resp = self.getresp()
[379] Fix | Delete
if resp[0] != '1':
[380] Fix | Delete
raise error_reply(resp)
[381] Fix | Delete
except:
[382] Fix | Delete
conn.close()
[383] Fix | Delete
raise
[384] Fix | Delete
else:
[385] Fix | Delete
with self.makeport() as sock:
[386] Fix | Delete
if rest is not None:
[387] Fix | Delete
self.sendcmd("REST %s" % rest)
[388] Fix | Delete
resp = self.sendcmd(cmd)
[389] Fix | Delete
# See above.
[390] Fix | Delete
if resp[0] == '2':
[391] Fix | Delete
resp = self.getresp()
[392] Fix | Delete
if resp[0] != '1':
[393] Fix | Delete
raise error_reply(resp)
[394] Fix | Delete
conn, sockaddr = sock.accept()
[395] Fix | Delete
if self.timeout is not _GLOBAL_DEFAULT_TIMEOUT:
[396] Fix | Delete
conn.settimeout(self.timeout)
[397] Fix | Delete
if resp[:3] == '150':
[398] Fix | Delete
# this is conditional in case we received a 125
[399] Fix | Delete
size = parse150(resp)
[400] Fix | Delete
return conn, size
[401] Fix | Delete
[402] Fix | Delete
def transfercmd(self, cmd, rest=None):
[403] Fix | Delete
"""Like ntransfercmd() but returns only the socket."""
[404] Fix | Delete
return self.ntransfercmd(cmd, rest)[0]
[405] Fix | Delete
[406] Fix | Delete
def login(self, user = '', passwd = '', acct = ''):
[407] Fix | Delete
'''Login, default anonymous.'''
[408] Fix | Delete
if not user:
[409] Fix | Delete
user = 'anonymous'
[410] Fix | Delete
if not passwd:
[411] Fix | Delete
passwd = ''
[412] Fix | Delete
if not acct:
[413] Fix | Delete
acct = ''
[414] Fix | Delete
if user == 'anonymous' and passwd in {'', '-'}:
[415] Fix | Delete
# If there is no anonymous ftp password specified
[416] Fix | Delete
# then we'll just use anonymous@
[417] Fix | Delete
# We don't send any other thing because:
[418] Fix | Delete
# - We want to remain anonymous
[419] Fix | Delete
# - We want to stop SPAM
[420] Fix | Delete
# - We don't want to let ftp sites to discriminate by the user,
[421] Fix | Delete
# host or country.
[422] Fix | Delete
passwd = passwd + 'anonymous@'
[423] Fix | Delete
resp = self.sendcmd('USER ' + user)
[424] Fix | Delete
if resp[0] == '3':
[425] Fix | Delete
resp = self.sendcmd('PASS ' + passwd)
[426] Fix | Delete
if resp[0] == '3':
[427] Fix | Delete
resp = self.sendcmd('ACCT ' + acct)
[428] Fix | Delete
if resp[0] != '2':
[429] Fix | Delete
raise error_reply(resp)
[430] Fix | Delete
return resp
[431] Fix | Delete
[432] Fix | Delete
def retrbinary(self, cmd, callback, blocksize=8192, rest=None):
[433] Fix | Delete
"""Retrieve data in binary mode. A new port is created for you.
[434] Fix | Delete
[435] Fix | Delete
Args:
[436] Fix | Delete
cmd: A RETR command.
[437] Fix | Delete
callback: A single parameter callable to be called on each
[438] Fix | Delete
block of data read.
[439] Fix | Delete
blocksize: The maximum number of bytes to read from the
[440] Fix | Delete
socket at one time. [default: 8192]
[441] Fix | Delete
rest: Passed to transfercmd(). [default: None]
[442] Fix | Delete
[443] Fix | Delete
Returns:
[444] Fix | Delete
The response code.
[445] Fix | Delete
"""
[446] Fix | Delete
self.voidcmd('TYPE I')
[447] Fix | Delete
with self.transfercmd(cmd, rest) as conn:
[448] Fix | Delete
while 1:
[449] Fix | Delete
data = conn.recv(blocksize)
[450] Fix | Delete
if not data:
[451] Fix | Delete
break
[452] Fix | Delete
callback(data)
[453] Fix | Delete
# shutdown ssl layer
[454] Fix | Delete
if _SSLSocket is not None and isinstance(conn, _SSLSocket):
[455] Fix | Delete
conn.unwrap()
[456] Fix | Delete
return self.voidresp()
[457] Fix | Delete
[458] Fix | Delete
def retrlines(self, cmd, callback = None):
[459] Fix | Delete
"""Retrieve data in line mode. A new port is created for you.
[460] Fix | Delete
[461] Fix | Delete
Args:
[462] Fix | Delete
cmd: A RETR, LIST, or NLST command.
[463] Fix | Delete
callback: An optional single parameter callable that is called
[464] Fix | Delete
for each line with the trailing CRLF stripped.
[465] Fix | Delete
[default: print_line()]
[466] Fix | Delete
[467] Fix | Delete
Returns:
[468] Fix | Delete
The response code.
[469] Fix | Delete
"""
[470] Fix | Delete
if callback is None:
[471] Fix | Delete
callback = print_line
[472] Fix | Delete
resp = self.sendcmd('TYPE A')
[473] Fix | Delete
with self.transfercmd(cmd) as conn, \
[474] Fix | Delete
conn.makefile('r', encoding=self.encoding) as fp:
[475] Fix | Delete
while 1:
[476] Fix | Delete
line = fp.readline(self.maxline + 1)
[477] Fix | Delete
if len(line) > self.maxline:
[478] Fix | Delete
raise Error("got more than %d bytes" % self.maxline)
[479] Fix | Delete
if self.debugging > 2:
[480] Fix | Delete
print('*retr*', repr(line))
[481] Fix | Delete
if not line:
[482] Fix | Delete
break
[483] Fix | Delete
if line[-2:] == CRLF:
[484] Fix | Delete
line = line[:-2]
[485] Fix | Delete
elif line[-1:] == '\n':
[486] Fix | Delete
line = line[:-1]
[487] Fix | Delete
callback(line)
[488] Fix | Delete
# shutdown ssl layer
[489] Fix | Delete
if _SSLSocket is not None and isinstance(conn, _SSLSocket):
[490] Fix | Delete
conn.unwrap()
[491] Fix | Delete
return self.voidresp()
[492] Fix | Delete
[493] Fix | Delete
def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
[494] Fix | Delete
"""Store a file in binary mode. A new port is created for you.
[495] Fix | Delete
[496] Fix | Delete
Args:
[497] Fix | Delete
cmd: A STOR command.
[498] Fix | Delete
fp: A file-like object with a read(num_bytes) method.
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function