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