在Python中尝试使用ftplib将文件上传到FTP服务器时出错

时间:2018-10-26 21:31:56

标签: python ftp-client ftplib

我正在尝试将文件上传到https://dlptest.com/ftp-test/中的测试FTP服务器。我正在ipython控制台中使用Python和ftplib,但是当我调用FTP.storlines('STORE file1.txt',open('file1.txt'))时,出现以下错误:

In [1]: import ftplib, os

In [2]: ftp = ftplib.FTP('ftp.dlptest.com')

In [3]: ftp.login('dlpuser@dlptest.com', 'e73jzTRTNqCN9PYAAjjn')
Out[3]: '230 OK. Current restricted directory is /'

In [4]: ftp.storlines('STOR file1.txt', open('file1.txt'))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-df84f4fce912> in <module>()
----> 1 ftp.storlines('STOR file1.txt', open('file1.txt'))

/usr/lib/python3.7/ftplib.py in storlines(self, cmd, fp, callback)
    536                     break
    537                 if buf[-2:] != B_CRLF:
--> 538                     if buf[-1] in B_CRLF: buf = buf[:-1]
    539                     buf = buf + B_CRLF
    540                 conn.sendall(buf)

TypeError: a bytes-like object is required, not 'str'

In [5]: 

当我做ftp.storlines('STOR file1.txt', open('file1.txt', 'rb')时:

In [5]: ftp.storlines('STOR file1.txt', open('file1.txt', 'rb'))
---------------------------------------------------------------------------
error_reply                               Traceback (most recent call last)
<ipython-input-5-afee52811a1f> in <module>()
----> 1 ftp.storlines('STOR file1.txt', open('file1.txt', 'rb'))

/usr/lib/python3.7/ftplib.py in storlines(self, cmd, fp, callback)
    528         """
    529         self.voidcmd('TYPE A')
--> 530         with self.transfercmd(cmd) as conn:
    531             while 1:
    532                 buf = fp.readline(self.maxline + 1)

/usr/lib/python3.7/ftplib.py in transfercmd(self, cmd, rest)
    397     def transfercmd(self, cmd, rest=None):
    398         """Like ntransfercmd() but returns only the socket."""
--> 399         return self.ntransfercmd(cmd, rest)[0]
    400 
    401     def login(self, user = '', passwd = '', acct = ''):

/usr/lib/python3.7/ftplib.py in ntransfercmd(self, cmd, rest)
    357         size = None
    358         if self.passiveserver:
--> 359             host, port = self.makepasv()
    360             conn = socket.create_connection((host, port), self.timeout,
    361                                             source_address=self.source_address)

/usr/lib/python3.7/ftplib.py in makepasv(self)
    335     def makepasv(self):
    336         if self.af == socket.AF_INET:
--> 337             host, port = parse227(self.sendcmd('PASV'))
    338         else:
    339             host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername())

/usr/lib/python3.7/ftplib.py in parse227(resp)
    843 
    844     if resp[:3] != '227':
--> 845         raise error_reply(resp)
    846     global _227_re
    847     if _227_re is None:

error_reply: 200 TYPE is now ASCII

您能帮我找出原因吗?预先谢谢你!

0 个答案:

没有答案