Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../usr/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
sys.audit("ftplib.connect", self, self.host, self.port)
[152] Fix | Delete
self.sock = socket.create_connection((self.host, self.port), self.timeout,
[153] Fix | Delete
source_address=self.source_address)
[154] Fix | Delete
self.af = self.sock.family
[155] Fix | Delete
self.file = self.sock.makefile('r', encoding=self.encoding)
[156] Fix | Delete
self.welcome = self.getresp()
[157] Fix | Delete
return self.welcome
[158] Fix | Delete
[159] Fix | Delete
def getwelcome(self):
[160] Fix | Delete
'''Get the welcome message from the server.
[161] Fix | Delete
(this is read and squirreled away by connect())'''
[162] Fix | Delete
if self.debugging:
[163] Fix | Delete
print('*welcome*', self.sanitize(self.welcome))
[164] Fix | Delete
return self.welcome
[165] Fix | Delete
[166] Fix | Delete
def set_debuglevel(self, level):
[167] Fix | Delete
'''Set the debugging level.
[168] Fix | Delete
The required argument level means:
[169] Fix | Delete
0: no debugging output (default)
[170] Fix | Delete
1: print commands and responses but not body text etc.
[171] Fix | Delete
2: also print raw lines read and sent before stripping CR/LF'''
[172] Fix | Delete
self.debugging = level
[173] Fix | Delete
debug = set_debuglevel
[174] Fix | Delete
[175] Fix | Delete
def set_pasv(self, val):
[176] Fix | Delete
'''Use passive or active mode for data transfers.
[177] Fix | Delete
With a false argument, use the normal PORT mode,
[178] Fix | Delete
With a true argument, use the PASV command.'''
[179] Fix | Delete
self.passiveserver = val
[180] Fix | Delete
[181] Fix | Delete
# Internal: "sanitize" a string for printing
[182] Fix | Delete
def sanitize(self, s):
[183] Fix | Delete
if s[:5] in {'pass ', 'PASS '}:
[184] Fix | Delete
i = len(s.rstrip('\r\n'))
[185] Fix | Delete
s = s[:5] + '*'*(i-5) + s[i:]
[186] Fix | Delete
return repr(s)
[187] Fix | Delete
[188] Fix | Delete
# Internal: send one line to the server, appending CRLF
[189] Fix | Delete
def putline(self, line):
[190] Fix | Delete
if '\r' in line or '\n' in line:
[191] Fix | Delete
raise ValueError('an illegal newline character should not be contained')
[192] Fix | Delete
sys.audit("ftplib.sendcmd", self, line)
[193] Fix | Delete
line = line + CRLF
[194] Fix | Delete
if self.debugging > 1:
[195] Fix | Delete
print('*put*', self.sanitize(line))
[196] Fix | Delete
self.sock.sendall(line.encode(self.encoding))
[197] Fix | Delete
[198] Fix | Delete
# Internal: send one command to the server (through putline())
[199] Fix | Delete
def putcmd(self, line):
[200] Fix | Delete
if self.debugging: print('*cmd*', self.sanitize(line))
[201] Fix | Delete
self.putline(line)
[202] Fix | Delete
[203] Fix | Delete
# Internal: return one line from the server, stripping CRLF.
[204] Fix | Delete
# Raise EOFError if the connection is closed
[205] Fix | Delete
def getline(self):
[206] Fix | Delete
line = self.file.readline(self.maxline + 1)
[207] Fix | Delete
if len(line) > self.maxline:
[208] Fix | Delete
raise Error("got more than %d bytes" % self.maxline)
[209] Fix | Delete
if self.debugging > 1:
[210] Fix | Delete
print('*get*', self.sanitize(line))
[211] Fix | Delete
if not line:
[212] Fix | Delete
raise EOFError
[213] Fix | Delete
if line[-2:] == CRLF:
[214] Fix | Delete
line = line[:-2]
[215] Fix | Delete
elif line[-1:] in CRLF:
[216] Fix | Delete
line = line[:-1]
[217] Fix | Delete
return line
[218] Fix | Delete
[219] Fix | Delete
# Internal: get a response from the server, which may possibly
[220] Fix | Delete
# consist of multiple lines. Return a single string with no
[221] Fix | Delete
# trailing CRLF. If the response consists of multiple lines,
[222] Fix | Delete
# these are separated by '\n' characters in the string
[223] Fix | Delete
def getmultiline(self):
[224] Fix | Delete
line = self.getline()
[225] Fix | Delete
if line[3:4] == '-':
[226] Fix | Delete
code = line[:3]
[227] Fix | Delete
while 1:
[228] Fix | Delete
nextline = self.getline()
[229] Fix | Delete
line = line + ('\n' + nextline)
[230] Fix | Delete
if nextline[:3] == code and \
[231] Fix | Delete
nextline[3:4] != '-':
[232] Fix | Delete
break
[233] Fix | Delete
return line
[234] Fix | Delete
[235] Fix | Delete
# Internal: get a response from the server.
[236] Fix | Delete
# Raise various errors if the response indicates an error
[237] Fix | Delete
def getresp(self):
[238] Fix | Delete
resp = self.getmultiline()
[239] Fix | Delete
if self.debugging:
[240] Fix | Delete
print('*resp*', self.sanitize(resp))
[241] Fix | Delete
self.lastresp = resp[:3]
[242] Fix | Delete
c = resp[:1]
[243] Fix | Delete
if c in {'1', '2', '3'}:
[244] Fix | Delete
return resp
[245] Fix | Delete
if c == '4':
[246] Fix | Delete
raise error_temp(resp)
[247] Fix | Delete
if c == '5':
[248] Fix | Delete
raise error_perm(resp)
[249] Fix | Delete
raise error_proto(resp)
[250] Fix | Delete
[251] Fix | Delete
def voidresp(self):
[252] Fix | Delete
"""Expect a response beginning with '2'."""
[253] Fix | Delete
resp = self.getresp()
[254] Fix | Delete
if resp[:1] != '2':
[255] Fix | Delete
raise error_reply(resp)
[256] Fix | Delete
return resp
[257] Fix | Delete
[258] Fix | Delete
def abort(self):
[259] Fix | Delete
'''Abort a file transfer. Uses out-of-band data.
[260] Fix | Delete
This does not follow the procedure from the RFC to send Telnet
[261] Fix | Delete
IP and Synch; that doesn't seem to work with the servers I've
[262] Fix | Delete
tried. Instead, just send the ABOR command as OOB data.'''
[263] Fix | Delete
line = b'ABOR' + B_CRLF
[264] Fix | Delete
if self.debugging > 1:
[265] Fix | Delete
print('*put urgent*', self.sanitize(line))
[266] Fix | Delete
self.sock.sendall(line, MSG_OOB)
[267] Fix | Delete
resp = self.getmultiline()
[268] Fix | Delete
if resp[:3] not in {'426', '225', '226'}:
[269] Fix | Delete
raise error_proto(resp)
[270] Fix | Delete
return resp
[271] Fix | Delete
[272] Fix | Delete
def sendcmd(self, cmd):
[273] Fix | Delete
'''Send a command and return the response.'''
[274] Fix | Delete
self.putcmd(cmd)
[275] Fix | Delete
return self.getresp()
[276] Fix | Delete
[277] Fix | Delete
def voidcmd(self, cmd):
[278] Fix | Delete
"""Send a command and expect a response beginning with '2'."""
[279] Fix | Delete
self.putcmd(cmd)
[280] Fix | Delete
return self.voidresp()
[281] Fix | Delete
[282] Fix | Delete
def sendport(self, host, port):
[283] Fix | Delete
'''Send a PORT command with the current host and the given
[284] Fix | Delete
port number.
[285] Fix | Delete
'''
[286] Fix | Delete
hbytes = host.split('.')
[287] Fix | Delete
pbytes = [repr(port//256), repr(port%256)]
[288] Fix | Delete
bytes = hbytes + pbytes
[289] Fix | Delete
cmd = 'PORT ' + ','.join(bytes)
[290] Fix | Delete
return self.voidcmd(cmd)
[291] Fix | Delete
[292] Fix | Delete
def sendeprt(self, host, port):
[293] Fix | Delete
'''Send an EPRT command with the current host and the given port number.'''
[294] Fix | Delete
af = 0
[295] Fix | Delete
if self.af == socket.AF_INET:
[296] Fix | Delete
af = 1
[297] Fix | Delete
if self.af == socket.AF_INET6:
[298] Fix | Delete
af = 2
[299] Fix | Delete
if af == 0:
[300] Fix | Delete
raise error_proto('unsupported address family')
[301] Fix | Delete
fields = ['', repr(af), host, repr(port), '']
[302] Fix | Delete
cmd = 'EPRT ' + '|'.join(fields)
[303] Fix | Delete
return self.voidcmd(cmd)
[304] Fix | Delete
[305] Fix | Delete
def makeport(self):
[306] Fix | Delete
'''Create a new socket and send a PORT command for it.'''
[307] Fix | Delete
sock = socket.create_server(("", 0), family=self.af, backlog=1)
[308] Fix | Delete
port = sock.getsockname()[1] # Get proper port
[309] Fix | Delete
host = self.sock.getsockname()[0] # Get proper host
[310] Fix | Delete
if self.af == socket.AF_INET:
[311] Fix | Delete
resp = self.sendport(host, port)
[312] Fix | Delete
else:
[313] Fix | Delete
resp = self.sendeprt(host, port)
[314] Fix | Delete
if self.timeout is not _GLOBAL_DEFAULT_TIMEOUT:
[315] Fix | Delete
sock.settimeout(self.timeout)
[316] Fix | Delete
return sock
[317] Fix | Delete
[318] Fix | Delete
def makepasv(self):
[319] Fix | Delete
"""Internal: Does the PASV or EPSV handshake -> (address, port)"""
[320] Fix | Delete
if self.af == socket.AF_INET:
[321] Fix | Delete
untrusted_host, port = parse227(self.sendcmd('PASV'))
[322] Fix | Delete
if self.trust_server_pasv_ipv4_address:
[323] Fix | Delete
host = untrusted_host
[324] Fix | Delete
else:
[325] Fix | Delete
host = self.sock.getpeername()[0]
[326] Fix | Delete
else:
[327] Fix | Delete
host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername())
[328] Fix | Delete
return host, port
[329] Fix | Delete
[330] Fix | Delete
def ntransfercmd(self, cmd, rest=None):
[331] Fix | Delete
"""Initiate a transfer over the data connection.
[332] Fix | Delete
[333] Fix | Delete
If the transfer is active, send a port command and the
[334] Fix | Delete
transfer command, and accept the connection. If the server is
[335] Fix | Delete
passive, send a pasv command, connect to it, and start the
[336] Fix | Delete
transfer command. Either way, return the socket for the
[337] Fix | Delete
connection and the expected size of the transfer. The
[338] Fix | Delete
expected size may be None if it could not be determined.
[339] Fix | Delete
[340] Fix | Delete
Optional `rest' argument can be a string that is sent as the
[341] Fix | Delete
argument to a REST command. This is essentially a server
[342] Fix | Delete
marker used to tell the server to skip over any data up to the
[343] Fix | Delete
given marker.
[344] Fix | Delete
"""
[345] Fix | Delete
size = None
[346] Fix | Delete
if self.passiveserver:
[347] Fix | Delete
host, port = self.makepasv()
[348] Fix | Delete
conn = socket.create_connection((host, port), self.timeout,
[349] Fix | Delete
source_address=self.source_address)
[350] Fix | Delete
try:
[351] Fix | Delete
if rest is not None:
[352] Fix | Delete
self.sendcmd("REST %s" % rest)
[353] Fix | Delete
resp = self.sendcmd(cmd)
[354] Fix | Delete
# Some servers apparently send a 200 reply to
[355] Fix | Delete
# a LIST or STOR command, before the 150 reply
[356] Fix | Delete
# (and way before the 226 reply). This seems to
[357] Fix | Delete
# be in violation of the protocol (which only allows
[358] Fix | Delete
# 1xx or error messages for LIST), so we just discard
[359] Fix | Delete
# this response.
[360] Fix | Delete
if resp[0] == '2':
[361] Fix | Delete
resp = self.getresp()
[362] Fix | Delete
if resp[0] != '1':
[363] Fix | Delete
raise error_reply(resp)
[364] Fix | Delete
except:
[365] Fix | Delete
conn.close()
[366] Fix | Delete
raise
[367] Fix | Delete
else:
[368] Fix | Delete
with self.makeport() as sock:
[369] Fix | Delete
if rest is not None:
[370] Fix | Delete
self.sendcmd("REST %s" % rest)
[371] Fix | Delete
resp = self.sendcmd(cmd)
[372] Fix | Delete
# See above.
[373] Fix | Delete
if resp[0] == '2':
[374] Fix | Delete
resp = self.getresp()
[375] Fix | Delete
if resp[0] != '1':
[376] Fix | Delete
raise error_reply(resp)
[377] Fix | Delete
conn, sockaddr = sock.accept()
[378] Fix | Delete
if self.timeout is not _GLOBAL_DEFAULT_TIMEOUT:
[379] Fix | Delete
conn.settimeout(self.timeout)
[380] Fix | Delete
if resp[:3] == '150':
[381] Fix | Delete
# this is conditional in case we received a 125
[382] Fix | Delete
size = parse150(resp)
[383] Fix | Delete
return conn, size
[384] Fix | Delete
[385] Fix | Delete
def transfercmd(self, cmd, rest=None):
[386] Fix | Delete
"""Like ntransfercmd() but returns only the socket."""
[387] Fix | Delete
return self.ntransfercmd(cmd, rest)[0]
[388] Fix | Delete
[389] Fix | Delete
def login(self, user = '', passwd = '', acct = ''):
[390] Fix | Delete
'''Login, default anonymous.'''
[391] Fix | Delete
if not user:
[392] Fix | Delete
user = 'anonymous'
[393] Fix | Delete
if not passwd:
[394] Fix | Delete
passwd = ''
[395] Fix | Delete
if not acct:
[396] Fix | Delete
acct = ''
[397] Fix | Delete
if user == 'anonymous' and passwd in {'', '-'}:
[398] Fix | Delete
# If there is no anonymous ftp password specified
[399] Fix | Delete
# then we'll just use anonymous@
[400] Fix | Delete
# We don't send any other thing because:
[401] Fix | Delete
# - We want to remain anonymous
[402] Fix | Delete
# - We want to stop SPAM
[403] Fix | Delete
# - We don't want to let ftp sites to discriminate by the user,
[404] Fix | Delete
# host or country.
[405] Fix | Delete
passwd = passwd + 'anonymous@'
[406] Fix | Delete
resp = self.sendcmd('USER ' + user)
[407] Fix | Delete
if resp[0] == '3':
[408] Fix | Delete
resp = self.sendcmd('PASS ' + passwd)
[409] Fix | Delete
if resp[0] == '3':
[410] Fix | Delete
resp = self.sendcmd('ACCT ' + acct)
[411] Fix | Delete
if resp[0] != '2':
[412] Fix | Delete
raise error_reply(resp)
[413] Fix | Delete
return resp
[414] Fix | Delete
[415] Fix | Delete
def retrbinary(self, cmd, callback, blocksize=8192, rest=None):
[416] Fix | Delete
"""Retrieve data in binary mode. A new port is created for you.
[417] Fix | Delete
[418] Fix | Delete
Args:
[419] Fix | Delete
cmd: A RETR command.
[420] Fix | Delete
callback: A single parameter callable to be called on each
[421] Fix | Delete
block of data read.
[422] Fix | Delete
blocksize: The maximum number of bytes to read from the
[423] Fix | Delete
socket at one time. [default: 8192]
[424] Fix | Delete
rest: Passed to transfercmd(). [default: None]
[425] Fix | Delete
[426] Fix | Delete
Returns:
[427] Fix | Delete
The response code.
[428] Fix | Delete
"""
[429] Fix | Delete
self.voidcmd('TYPE I')
[430] Fix | Delete
with self.transfercmd(cmd, rest) as conn:
[431] Fix | Delete
while 1:
[432] Fix | Delete
data = conn.recv(blocksize)
[433] Fix | Delete
if not data:
[434] Fix | Delete
break
[435] Fix | Delete
callback(data)
[436] Fix | Delete
# shutdown ssl layer
[437] Fix | Delete
if _SSLSocket is not None and isinstance(conn, _SSLSocket):
[438] Fix | Delete
conn.unwrap()
[439] Fix | Delete
return self.voidresp()
[440] Fix | Delete
[441] Fix | Delete
def retrlines(self, cmd, callback = None):
[442] Fix | Delete
"""Retrieve data in line mode. A new port is created for you.
[443] Fix | Delete
[444] Fix | Delete
Args:
[445] Fix | Delete
cmd: A RETR, LIST, or NLST command.
[446] Fix | Delete
callback: An optional single parameter callable that is called
[447] Fix | Delete
for each line with the trailing CRLF stripped.
[448] Fix | Delete
[default: print_line()]
[449] Fix | Delete
[450] Fix | Delete
Returns:
[451] Fix | Delete
The response code.
[452] Fix | Delete
"""
[453] Fix | Delete
if callback is None:
[454] Fix | Delete
callback = print_line
[455] Fix | Delete
resp = self.sendcmd('TYPE A')
[456] Fix | Delete
with self.transfercmd(cmd) as conn, \
[457] Fix | Delete
conn.makefile('r', encoding=self.encoding) as fp:
[458] Fix | Delete
while 1:
[459] Fix | Delete
line = fp.readline(self.maxline + 1)
[460] Fix | Delete
if len(line) > self.maxline:
[461] Fix | Delete
raise Error("got more than %d bytes" % self.maxline)
[462] Fix | Delete
if self.debugging > 2:
[463] Fix | Delete
print('*retr*', repr(line))
[464] Fix | Delete
if not line:
[465] Fix | Delete
break
[466] Fix | Delete
if line[-2:] == CRLF:
[467] Fix | Delete
line = line[:-2]
[468] Fix | Delete
elif line[-1:] == '\n':
[469] Fix | Delete
line = line[:-1]
[470] Fix | Delete
callback(line)
[471] Fix | Delete
# shutdown ssl layer
[472] Fix | Delete
if _SSLSocket is not None and isinstance(conn, _SSLSocket):
[473] Fix | Delete
conn.unwrap()
[474] Fix | Delete
return self.voidresp()
[475] Fix | Delete
[476] Fix | Delete
def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
[477] Fix | Delete
"""Store a file in binary mode. A new port is created for you.
[478] Fix | Delete
[479] Fix | Delete
Args:
[480] Fix | Delete
cmd: A STOR command.
[481] Fix | Delete
fp: A file-like object with a read(num_bytes) method.
[482] Fix | Delete
blocksize: The maximum data size to read from fp and send over
[483] Fix | Delete
the connection at once. [default: 8192]
[484] Fix | Delete
callback: An optional single parameter callable that is called on
[485] Fix | Delete
each block of data after it is sent. [default: None]
[486] Fix | Delete
rest: Passed to transfercmd(). [default: None]
[487] Fix | Delete
[488] Fix | Delete
Returns:
[489] Fix | Delete
The response code.
[490] Fix | Delete
"""
[491] Fix | Delete
self.voidcmd('TYPE I')
[492] Fix | Delete
with self.transfercmd(cmd, rest) as conn:
[493] Fix | Delete
while 1:
[494] Fix | Delete
buf = fp.read(blocksize)
[495] Fix | Delete
if not buf:
[496] Fix | Delete
break
[497] Fix | Delete
conn.sendall(buf)
[498] Fix | Delete
if callback:
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function