无法使用Apache Airflow中的FTPHook连接到FTP服务器

时间:2019-11-12 18:07:02

标签: ftp airflow airflow-operator

我正在尝试在Apache Airflow中使用FTPHook类。以下是以ftp.gnu.org为例的测试代码:

from airflow.contrib.hooks.ftp_hook import FTPHook
hook = FTPHook('gnu')
print(hook.list_directory(''))

此代码返回错误:

{base_hook.py:84} INFO - Using connection to: id: gnu. Host: ftp.gnu.org, Port: None, Schema: None, Login: None, Password: None, extra: {}
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    print(hook.list_directory(''))
  File "/home/ubuntu/.anaconda3/envs/airflow/lib/python3.7/site-packages/airflow/contrib/hooks/ftp_hook.py", line 127, in list_directory
    conn.cwd(path)
  File "/home/ubuntu/.anaconda3/envs/airflow/lib/python3.7/ftplib.py", line 631, in cwd
    return self.voidcmd(cmd)
  File "/home/ubuntu/.anaconda3/envs/airflow/lib/python3.7/ftplib.py", line 278, in voidcmd
    return self.voidresp()
  File "/home/ubuntu/.anaconda3/envs/airflow/lib/python3.7/ftplib.py", line 251, in voidresp
    resp = self.getresp()
  File "/home/ubuntu/.anaconda3/envs/airflow/lib/python3.7/ftplib.py", line 246, in getresp
    raise error_perm(resp)
ftplib.error_perm: 530 Please login with USER and PASS.

但是,如果我们将这行代码hook.get_conn().login()放在print函数之前,则一切正常。这是错误还是我做错了什么?

1 个答案:

答案 0 :(得分:1)

由于Slack中的Apache Airflow社区,我们可以通过在Airflow GUI平台中设置适当的用户名和密码来解决该问题。尽管我试图以匿名用户身份进行连接,但必须设置 username ='anonymous' password ='anonymous @'。在docs的第一个示例中,这似乎是 ftplib 的默认设置,但是如果您离开,Airflow会将这些值更改为 None 他们是空的。