我想写一个脚本来连接我的FTP服务器,但它对我不起作用。
from ftplib import FTP
ftp=FTP()
ftp.set_debuglevel(2)
ftp.connect('192.169.137.100')
ftp.login('test','test')
ftp.dir()
ftp.close()
当构建这个脚本时,我得到了这些信息
*get* '220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------\r\n'
*get* '220-You are user number 1 of 50 allowed.\r\n'
*get* '220-Local time is now 23:46. Server port: 21.\r\n'
*get* '220-This is a private system - No anonymous login\r\n'
*get* '220 You will be disconnected after 15 minutes of inactivity.\r\n'
*resp* '220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------\n220-You are user number 1 of 50 allowed.\n220-Local time is now 23:46. Server port: 21.\n220-This is a private system - No anonymous login\n220 You will be disconnected after 15 minutes of inactivity.'
*cmd* 'USER test'
*put* 'USER test\r\n'
*get* '331 User test OK. Password required\r\n'
*resp* '331 User test OK. Password required'
*cmd* 'PASS ****'
*put* 'PASS ****\r\n'
*getTraceback (most recent call last):
File "D:\photoWebSite\py_test.py", line 9, in <module>
* '530 Login authentication failed\r\n'
*resp* '530 Login authentication failed'
ftp.login('test','test')
File "D:\LinuxSL\python27\lib\ftplib.py", line 393, in login
if resp[0] == '3': resp = self.sendcmd('PASS ' + passwd)
File "D:\LinuxSL\python27\lib\ftplib.py", line 249, in sendcmd
return self.getresp()
File "D:\LinuxSL\python27\lib\ftplib.py", line 224, in getresp
raise error_perm, resp
ftplib.error_perm: 530 Login authentication failed
我希望得到一些帮助。
答案 0 :(得分:3)
您可以尝试使用FTP_TLS
,如下所示:
from ftplib import FTP_TLS
ftp=FTP_TLS()
ftp.set_debuglevel(2)
ftp.connect('192.169.137.100', 22)
ftp.sendcmd('USER test')
ftp.sendcmd('PASS test')
ftp.dir()
ftp.close()
答案 1 :(得分:1)
我使用了ftplib
,并且由于error_perm
而无法连接,使用了pysftp
,它对我有用。
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
myHostname = "abc.org"
myUsername = "username"
myPassword = "password"
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts) as sftp:
print ("Connection succesfully stablished ... ")
directory_structure = sftp.listdir_attr()
for attr in directory_structure:
print (attr.filename, attr)
答案 2 :(得分:0)
确保服务器支持凭据登录!
有可能它仅支持匿名登录
使用:
def connect(host, username, passwd, port):
from ftplib import FTP
print(f'host:{host}, username:{username} passwd:{passwd}, port: {port}')
#instance of ftp
ftp = FTP()
#set the debig level
ftp.set_debuglevel(2)
#connect to ftp
ftp.connect(host=str(host), port=int(port))
print('connected')
#login
print('login in.....')
if username:
if passwd:
ftp.login(user=username, passwd=passwd)
else:
print('Password required')
exit(0)
else:
#login without username and password
ftp.login()
#return the dirs list and ftp object
dirs = list()
ftp.retrlines("LIST", dirs.append())
return ftp, dirs
上面的代码验证用户是否通过了用户名和密码,如果是,则使用凭据登录。如果不是匿名登录,但是我建议您首先找出并启用服务器的凭据登录。尝试使用python ftp服务器在https://github.com/malgulam/100ProjectsOfCode/blob/main/ftpclient/ftpclient_simple/ftp_server.py和https://github.com/keepitsimple/pyFTPclient处启动您的客户端并使用上面的connect函数。