ftplib:被动& NAT /地址转换

时间:2012-09-13 17:46:49

标签: python python-2.7 ftplib

有没有办法检测服务器在进入被动模式时返回其专用网络地址,并将其转换为公共地址?
我无法访问服务器。

>>> # Python 2.7.3rc2
>>> from ftplib import FTP_TLS
>>> ftps = FTP_TLS()
>>> ftps.set_debuglevel(2)
>>> ftps.connect("example.com", 123)
*get* '220 Welcome to Synchrony Gateway FTP server\r\n'
*resp* '220 Welcome to Synchrony Gateway FTP server'
'220 Welcome to Synchrony Gateway FTP server'

>>> ftps.auth()
*cmd* 'AUTH TLS'
*put* 'AUTH TLS\r\n'
*get* '234 Using authentication type TLS\r\n'
*resp* '234 Using authentication type TLS'
'234 Using authentication type TLS'

>>> ftps.login("foo", "***")
*cmd* 'AUTH TLS'
*put* 'AUTH TLS\r\n'
*get* '234 AUTH command OK, waiting handshake\r\n'
*resp* '234 AUTH command OK, waiting handshake'
*cmd* 'USER foo'
*put* 'USER foo\r\n'
*get* '331 Send password please\r\n'
*resp* '331 Send password please'
*cmd* 'PASS ************'
*put* 'PASS ************\r\n'
*get* '230 User logged in, proceed\r\n'
*resp* '230 User logged in, proceed'
'230 User logged in, proceed'

>>> ftps.prot_p()
*cmd* 'PBSZ 0'
*put* 'PBSZ 0\r\n'
*get* '200 Command okay\r\n'
*resp* '200 Command okay'
*cmd* 'PROT P'
*put* 'PROT P\r\n'
*get* '200 Command okay\r\n'
*resp* '200 Command okay'
'200 Command okay'

>>> ftps.set_pasv(True)
>>> ftps.retrlines("LIST")
*cmd* 'TYPE A'
*put* 'TYPE A\r\n'
*get* '200 Type set to A\r\n'
*resp* '200 Type set to A'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '227 Entering Passive Mode (192,168,17,1,195,119)\r\n'
*resp* '227 Entering Passive Mode (192,168,17,1,195,119)'

感谢。

2 个答案:

答案 0 :(得分:2)

我遇到了类似的问题。这是覆盖远程主机返回的本地IP的快速方法:

from ftplib import FTP, parse227, parse229
class MyFTP(FTP):
    def makepasv(self):
        if self.af == socket.AF_INET:
            host, port = parse227(self.sendcmd('PASV'))
        else:
            host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername())
        # We tweak this so we don't use the internal ip returned by the remote server.
        # old: `return host, port`
        return self.host, port

请注意,您不能使用super(),因为FTP是一种旧版式的' python类。

答案 1 :(得分:1)

将服务器配置为在外部IP上进入被动模式。