【问题标题】:How do I access pyftpdlib FTPS server on Amazon EC2?如何访问 Amazon EC2 上的 pyftpdlib FTPS 服务器?
【发布时间】:2016-03-13 10:13:01
【问题描述】:

我正在尝试使用 Python 库 pyftpdlib 在我的 Ubuntu Amazon EC2 实例上创建一个简单的 FTPS 服务器。

这是直接来自文档的代码:

#!/usr/bin/env python

"""
An RFC-4217 asynchronous FTPS server supporting both SSL and TLS.
Requires PyOpenSSL module (http://pypi.python.org/pypi/pyOpenSSL).
"""

from pyftpdlib.servers import FTPServer
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.contrib.handlers import TLS_FTPHandler
import os


def main():
    authorizer = DummyAuthorizer()
    authorizer.add_user('ubuntu', '*****', os.getcwd(), perm='elradfmw')
    authorizer.add_anonymous('.')
    handler = TLS_FTPHandler
    handler.certfile = 'keycert.pem'
    handler.authorizer = authorizer
    handler.masquerade_address = '52.23.244.142'
    # requires SSL for both control and data channel
    handler.tls_control_required = True
    handler.tls_data_required = True
    handler.passive_ports = range(60000, 60099)
    server = FTPServer(('', 21), handler)
    server.serve_forever()

if __name__ == '__main__':
    main()

当我在我的 Amazon EC2 实例上运行脚本并尝试使用 FileZilla 远程连接时,我得到:

Status: Connecting to 52.23.244.142:21...
Status: Connection established, waiting for welcome message...
Response:   220 pyftpdlib 1.4.0 ready.
Command:    AUTH TLS
Response:   234 AUTH TLS successful.
Status: Initializing TLS...
Status: Verifying certificate...
Command:    USER ubuntu
Status: TLS/SSL connection established.
Response:   331 Username ok, send password.
Command:    PASS *****
Response:   230 Login successful.
Command:    OPTS UTF8 ON
Response:   501 Invalid argument.
Command:    PBSZ 0
Response:   200 PBSZ=0 successful.
Command:    PROT P
Response:   200 Protection set to Private
Command:    OPTS MLST type;perm;size;modify;unix.mode;unix.uid;unix.gid;
Response:   200 MLST OPTS type;perm;size;modify;unix.mode;unix.uid;unix.gid;
Status: Connected
Status: Retrieving directory listing...
Command:    PWD
Response:   257 "/" is the current directory.
Command:    TYPE I
Response:   200 Type set to: Binary.
Command:    PASV
Response:   227 Entering passive mode (52,23,244,142,174,172).
Command:    MLSD
Response:   150 File status okay. About to open data connection.
Error:  Connection timed out
Error:  Failed to retrieve directory listing

我想我错过了什么。我能得到一些帮助吗?

【问题讨论】:

  • 有什么问题/疑问?
  • @MartinPrikryl 我可以访问在我的本地机器上运行的同一个 ftp 服务器。但是无法访问我的ec2上运行的ftp服务器。为什么?
  • 为什么?发生什么了?有任何错误信息吗?
  • @MartinPrikryl 刚刚在帖子末尾添加了错误消息。

标签: python amazon-ec2 ftp ftp-server pyftpdlib


【解决方案1】:
  1. 您的服务器必须在对PASV 命令的响应中提供其外部 IP 地址。相反,您在 EC2 专用网络中提供了一个内部 IP 地址,FileZilla 显然无法连接到该地址。

    虽然 FileZilla 可以解决这个问题:

    服务器发送了带有不可路由地址的被动回复。改为使用服务器地址。

    其他 FTP 客户端(如 Windows 命令行 ftp.exe)不能。

    使用handler.masquerade_address配置外部IP地址:

    handler.masquerade_address = '52.23.244.142'
    
  2. FileZilla 无法连接到端口 50048 (195

    参见Setting up FTP on Amazon Cloud Server(尤其是the best answer 中的“步骤#2:打开EC2 实例上的FTP 端口”部分)。

    为避免打开整个非特权端口范围,请使用handler.passive_ports 限制 FTP 服务器使用较小的端口范围:

    handler.passive_ports = range(60000, 60099)
    

有关一般信息,请参阅我的关于network setup in respect to FTP passive (and active) connection modes 的文章。

【讨论】:

  • 假设我也有一个位于 52.23.244.142 的网站。我将 ftp 客户端指向哪个确切地址以进行传输。是52.23.244.142:20 还是52.23.244.142
  • @Skyler 地址是52.23.244.142。 20是港口。但是 FTP 端口是 21,而不是 20。并且 FTP 客户端会默认使用它。如果您还有其他问题,请在适当的网站上发布单独的问题(例如 Super User)。
猜你喜欢
  • 1970-01-01
  • 2021-05-06
  • 1970-01-01
  • 2014-04-14
  • 2012-05-09
  • 2014-12-22
  • 2014-01-27
  • 2021-10-04
  • 2015-02-19
相关资源
最近更新 更多