418 def retrlines(self, cmd, callback = None):
419 """Retrieve data in line mode. A new port is created for you.
420
421 Args:
422 cmd: A RETR, LIST, NLST, or MLSD command.
423 callback: An optional single parameter callable that is called
424 for each line with the trailing CRLF stripped.
425 [default: print_line()]
426
427 Returns:
428 The response code.
429 """
430 if callback is None: callback = print_line
431 resp = self.sendcmd('TYPE A')
432 conn = self.transfercmd(cmd)
433 fp = conn.makefile('rb')
434 while 1:
435 line = fp.readline()
436 if self.debugging > 2: print '*retr*', repr(line)
437 if not line:
438 break
439 if line[-2:] == CRLF:
440 line = line[:-2]
441 elif line[-1:] == '\n':
442 line = line[:-1]
443 callback(line)
444 fp.close()
445 conn.close()
446 return self.voidresp()
正如其他答案所解释的,函数retrlines()本身发出一个调用,该调用打印到stdout。相关的行是430,其中默认情况下变量callback被定义为函数print_line(),这并不奇怪,它只是打印一个给定的字符串:
^{pr2}$
在retrlines()的第443行调用callback()函数,导致打印该行。在
要禁止打印输出,可以使用retrlines()和一个自定义的callback函数一起使用,该函数不执行任何操作,例如ftp.retrlines('LIST', callback=lambda x: pass)