wrap some things in with blocks

This commit is contained in:
Benjamin Peterson 2010-10-31 18:21:16 +00:00
parent 2d8c917f30
commit a807137c39

View file

@ -406,13 +406,12 @@ class FTP:
The response code. The response code.
""" """
self.voidcmd('TYPE I') self.voidcmd('TYPE I')
conn = self.transfercmd(cmd, rest) with self.transfercmd(cmd, rest) as conn:
while 1: while 1:
data = conn.recv(blocksize) data = conn.recv(blocksize)
if not data: if not data:
break break
callback(data) callback(data)
conn.close()
return self.voidresp() return self.voidresp()
def retrlines(self, cmd, callback = None): def retrlines(self, cmd, callback = None):
@ -429,20 +428,18 @@ class FTP:
""" """
if callback is None: callback = print_line if callback is None: callback = print_line
resp = self.sendcmd('TYPE A') resp = self.sendcmd('TYPE A')
conn = self.transfercmd(cmd) with self.transfercmd(cmd) as conn, \
fp = conn.makefile('r', encoding=self.encoding) conn.makefile('r', encoding=self.encoding) as fp:
while 1: while 1:
line = fp.readline() line = fp.readline()
if self.debugging > 2: print('*retr*', repr(line)) if self.debugging > 2: print('*retr*', repr(line))
if not line: if not line:
break break
if line[-2:] == CRLF: if line[-2:] == CRLF:
line = line[:-2] line = line[:-2]
elif line[-1:] == '\n': elif line[-1:] == '\n':
line = line[:-1] line = line[:-1]
callback(line) callback(line)
fp.close()
conn.close()
return self.voidresp() return self.voidresp()
def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None): def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
@ -461,13 +458,12 @@ class FTP:
The response code. The response code.
""" """
self.voidcmd('TYPE I') self.voidcmd('TYPE I')
conn = self.transfercmd(cmd, rest) with self.transfercmd(cmd, rest) as conn:
while 1: while 1:
buf = fp.read(blocksize) buf = fp.read(blocksize)
if not buf: break if not buf: break
conn.sendall(buf) conn.sendall(buf)
if callback: callback(buf) if callback: callback(buf)
conn.close()
return self.voidresp() return self.voidresp()
def storlines(self, cmd, fp, callback=None): def storlines(self, cmd, fp, callback=None):
@ -483,16 +479,15 @@ class FTP:
The response code. The response code.
""" """
self.voidcmd('TYPE A') self.voidcmd('TYPE A')
conn = self.transfercmd(cmd) with self.transfercmd(cmd) as conn:
while 1: while 1:
buf = fp.readline() buf = fp.readline()
if not buf: break if not buf: break
if buf[-2:] != B_CRLF: if buf[-2:] != B_CRLF:
if buf[-1] in B_CRLF: buf = buf[:-1] if buf[-1] in B_CRLF: buf = buf[:-1]
buf = buf + B_CRLF buf = buf + B_CRLF
conn.sendall(buf) conn.sendall(buf)
if callback: callback(buf) if callback: callback(buf)
conn.close()
return self.voidresp() return self.voidresp()
def acct(self, password): def acct(self, password):