我试图在2分钟内重新连接到FTP服务器,以便访问新生成的文件。但我面临属性错误。无法识别问题。任何人都建议正确的重新连接方式到FTP服务器。
代码:
def Connect1():
ftp=FTP('ipaddress')
ftp.login(username,password)
files=ftp.dir()
for file_ in natsorted(files):
if file_.endswith('.sec.gz'):
file_c=file_.split('/')
file_d=file_c[3]
print file_d
files=date_b+'\\'+file_d
print files
ftp.retrbinary('RETR files', open('C:\\test\\'+file_d, 'wb').write)
ftp.quit()
time.sleep(120)
Connect1()
结果:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-20-00293e7b319b> in <module>()
----> 1 Connect()
2
3
4
<ipython-input-19-0bc6dc053f87> in Connect()
49 #output_filename1 = os.path.join(files)
50 #print output_filename1
---> 51 ftp.retrbinary('RETR FAO05/DATA_INTV_NEW/'+files, open('C:\\test\\'+file_d, 'wb').write)
52 ftp.quit()
53 time.sleep(110)
C:\Python27\Lib\ftplib.pyc in retrbinary(self, cmd, callback, blocksize, rest)
411 The response code.
412 """
--> 413 self.voidcmd('TYPE I')
414 conn = self.transfercmd(cmd, rest)
415 while 1:
C:\Python27\Lib\ftplib.pyc in voidcmd(self, cmd)
251 def voidcmd(self, cmd):
252 """Send a command and expect a response beginning with '2'."""
--> 253 self.putcmd(cmd)
254 return self.voidresp()
255
C:\Python27\Lib\ftplib.pyc in putcmd(self, line)
179 def putcmd(self, line):
180 if self.debugging: print '*cmd*', self.sanitize(line)
--> 181 self.putline(line)
182
183 # Internal: return one line from the server, stripping CRLF.
C:\Python27\Lib\ftplib.pyc in putline(self, line)
174 line = line + CRLF
175 if self.debugging > 1: print '*put*', self.sanitize(line)
--> 176 self.sock.sendall(line)
177
178 # Internal: send one command to the server (through putline())
AttributeError: 'NoneType' object has no attribute 'sendall'
答案 0 :(得分:3)
部分是一个疯狂的猜测,因为你的代码的缩进完全被破坏了,但如果对ftp.quit()
的调用真的在你的for循环中,那么难怪你在第二次迭代时会得到这样的错误。如果你真的想在每次迭代之间断开与服务器的连接,那么你显然必须在下一次迭代开始时重新连接......